Skip to content

Commit

Permalink
Fixed bug: SemaphoreNeighborInject transformations were implemented a…
Browse files Browse the repository at this point in the history
…mbiguously (some tools used route-semaphore matches, some used route matches)
  • Loading branch information
szarnyasg committed Nov 21, 2016
1 parent 5ee6a14 commit af2b6f3
Show file tree
Hide file tree
Showing 43 changed files with 234 additions and 117 deletions.
Expand Up @@ -13,19 +13,27 @@

import hu.bme.mit.trainbenchmark.benchmark.matches.SemaphoreNeighborInjectMatch;
import hu.bme.mit.trainbenchmark.railway.Route;
import hu.bme.mit.trainbenchmark.railway.Semaphore;

public class EmfSemaphoreNeighborInjectMatch extends EmfMatch implements SemaphoreNeighborInjectMatch {

protected final Route route;
protected final Semaphore semaphore;

public EmfSemaphoreNeighborInjectMatch(final Route route) {
public EmfSemaphoreNeighborInjectMatch(final Route route, final Semaphore semaphore) {
super();
this.route = route;
this.semaphore = semaphore;
}

@Override
public Route getRoute() {
return route;
}

@Override
public Semaphore getSemaphore() {
return semaphore;
}

}
Expand Up @@ -30,7 +30,6 @@ public void activate(final Collection<TPosLengthInjectMatch> matches) throws IOE
for (final TPosLengthInjectMatch match : matches) {
match.getSegment().setLength(0);
}
System.out.println(matches.size());
}

}
Expand Up @@ -17,6 +17,8 @@
import hu.bme.mit.trainbenchmark.benchmark.emf.driver.EmfDriver;
import hu.bme.mit.trainbenchmark.benchmark.emf.matches.EmfSemaphoreNeighborInjectMatch;
import hu.bme.mit.trainbenchmark.benchmark.emf.transformation.EmfTransformation;
import hu.bme.mit.trainbenchmark.railway.Route;
import hu.bme.mit.trainbenchmark.railway.Semaphore;

public class EmfTransformationInjectSemaphoreNeighbor<TDriver extends EmfDriver, TSemaphoreNeighborInjectMatch extends EmfSemaphoreNeighborInjectMatch>
extends EmfTransformation<TSemaphoreNeighborInjectMatch, TDriver> {
Expand All @@ -28,7 +30,11 @@ public EmfTransformationInjectSemaphoreNeighbor(final TDriver driver) {
@Override
public void activate(final Collection<TSemaphoreNeighborInjectMatch> matches) throws IOException {
for (final TSemaphoreNeighborInjectMatch match : matches) {
match.getRoute().setEntry(null);
final Route route = match.getRoute();
final Semaphore semaphore = match.getSemaphore();
if (route.getEntry().equals(semaphore)) {
route.setEntry(null);
}
}
}
}
Expand Up @@ -20,6 +20,7 @@
import hu.bme.mit.trainbenchmark.benchmark.emf.matches.EmfSemaphoreNeighborInjectMatch;
import hu.bme.mit.trainbenchmark.constants.RailwayQuery;
import hu.bme.mit.trainbenchmark.railway.Route;
import hu.bme.mit.trainbenchmark.railway.Semaphore;

public class EmfApiQuerySemaphoreNeighborInject<TDriver extends EmfDriver> extends EmfApiQuery<EmfSemaphoreNeighborInjectMatch, TDriver> {

Expand All @@ -32,9 +33,14 @@ public Collection<EmfSemaphoreNeighborInjectMatch> evaluate() {
final List<EmfSemaphoreNeighborInjectMatch> matches = new ArrayList<>();

for (final Route route : driver.getContainer().getRoutes()) {
matches.add(new EmfSemaphoreNeighborInjectMatch(route));
final Semaphore entry = route.getEntry();
if (entry == null) {
continue;
}

matches.add(new EmfSemaphoreNeighborInjectMatch(route, entry));
}

return matches;
}

Expand Down
Expand Up @@ -25,4 +25,9 @@ public Long getRoute() {
return (Long) qs.get("route").get();
}

@Override
public Object getSemaphore() {
return (Long) qs.get("semaphore").get();
}

}
Expand Up @@ -12,6 +12,7 @@
package hu.bme.mit.trainbenchmark.benchmark.jena.matches;

import static hu.bme.mit.trainbenchmark.constants.QueryConstants.VAR_ROUTE;
import static hu.bme.mit.trainbenchmark.constants.QueryConstants.VAR_SEMAPHORE;

import org.apache.jena.query.QuerySolution;
import org.apache.jena.rdf.model.Resource;
Expand All @@ -29,4 +30,9 @@ public Resource getRoute() {
return qs.getResource(VAR_ROUTE);
}

@Override
public Resource getSemaphore() {
return qs.getResource(VAR_SEMAPHORE);
}

}
Expand Up @@ -44,7 +44,7 @@ public void activate(final Collection<JenaConnectedSegmentsInjectMatch> matches)

for (final JenaConnectedSegmentsInjectMatch csim : matches) {
// create (segment2) node
final Long newVertexId = driver.getNewVertexId();
final Long newVertexId = driver.generateNewVertexId();
final Resource segment2 = model.createResource(BASE_PREFIX + ID_PREFIX + newVertexId);
model.add(model.createStatement(segment2, RDF.type, segmentType));
model.add(model.createLiteralStatement(segment2, length, TrainBenchmarkConstants.DEFAULT_SEGMENT_LENGTH));
Expand Down
Expand Up @@ -11,18 +11,17 @@
*******************************************************************************/
package hu.bme.mit.trainbenchmark.benchmark.jena.transformations.inject;

import static hu.bme.mit.trainbenchmark.constants.ModelConstants.ENTRY;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;

import hu.bme.mit.trainbenchmark.benchmark.jena.driver.JenaDriver;
import hu.bme.mit.trainbenchmark.benchmark.jena.matches.JenaSemaphoreNeighborInjectMatch;
import hu.bme.mit.trainbenchmark.benchmark.jena.transformations.JenaTransformation;
import hu.bme.mit.trainbenchmark.constants.ModelConstants;
import hu.bme.mit.trainbenchmark.rdf.RdfConstants;

public class JenaTransformationInjectSemaphoreNeighbor extends JenaTransformation<JenaSemaphoreNeighborInjectMatch> {

Expand All @@ -32,8 +31,11 @@ public JenaTransformationInjectSemaphoreNeighbor(final JenaDriver driver) {

@Override
public void activate(final Collection<JenaSemaphoreNeighborInjectMatch> matches) throws IOException {
final List<Resource> routes = matches.stream().map(it -> it.getRoute()).collect(Collectors.toList());
driver.deleteOutgoingEdges(routes, ENTRY);
final Model model = driver.getModel();
final Property entry = model.getProperty(RdfConstants.BASE_PREFIX + ModelConstants.ENTRY);
for (JenaSemaphoreNeighborInjectMatch match : matches) {
model.remove(match.getRoute(), entry, match.getSemaphore());
}
}

}
Expand Up @@ -41,7 +41,7 @@ public void activate(final Collection<JenaSwitchMonitoredMatch> matches) throws

for (final JenaSwitchMonitoredMatch match : matches) {
final Resource sw = match.getSw();
final Long newVertexId = driver.getNewVertexId();
final Long newVertexId = driver.generateNewVertexId();
final Resource sensor = model.createResource(BASE_PREFIX + ID_PREFIX + newVertexId);

model.add(model.createStatement(sw, sensorEdge, sensor));
Expand Down
5 changes: 5 additions & 0 deletions trainbenchmark-tool-neo4j/README.md
@@ -0,0 +1,5 @@
# Neo4j implementation

Neo4j cannot assign a specific id to a new node.

This means that if we run multiple transformations (e.g. ConnectedSegmentsInject, which inserts new Segments), we cannot guarantee the precise number of matches.
Expand Up @@ -12,6 +12,7 @@
package hu.bme.mit.trainbenchmark.benchmark.neo4j.matches;

import static hu.bme.mit.trainbenchmark.constants.QueryConstants.VAR_ROUTE;
import static hu.bme.mit.trainbenchmark.constants.QueryConstants.VAR_SEMAPHORE;

import java.util.Map;

Expand All @@ -30,4 +31,9 @@ public Node getRoute() {
return (Node) match.get(VAR_ROUTE);
}

@Override
public Node getSemaphore() {
return (Node) match.get(VAR_SEMAPHORE);
}

}
Expand Up @@ -16,8 +16,10 @@
import java.util.HashMap;
import java.util.Map;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;

import hu.bme.mit.trainbenchmark.benchmark.neo4j.driver.Neo4jDriver;
Expand All @@ -41,9 +43,16 @@ public Collection<Neo4jSemaphoreNeighborInjectMatch> evaluate() {
// (route:Route)
final Iterable<Node> routes = () -> graphDb.findNodes(Neo4jConstants.labelRoute);
for (final Node route : routes) {
final Map<String, Object> match = new HashMap<>();
match.put(QueryConstants.VAR_ROUTE, route);
matches.add(new Neo4jSemaphoreNeighborInjectMatch(match));
Iterable<Relationship> entries = route.getRelationships(Direction.OUTGOING, Neo4jConstants.relationshipTypeEntry);

for (Relationship entry : entries) {
final Node semaphore = entry.getEndNode();

final Map<String, Object> match = new HashMap<>();
match.put(QueryConstants.VAR_ROUTE, route);
match.put(QueryConstants.VAR_SEMAPHORE, semaphore);
matches.add(new Neo4jSemaphoreNeighborInjectMatch(match));
}
}
}

Expand Down
Expand Up @@ -38,7 +38,7 @@ public Collection<Neo4jSwitchMonitoredInjectMatch> evaluate() {
final Collection<Neo4jSwitchMonitoredInjectMatch> matches = new ArrayList<>();

final GraphDatabaseService graphDb = driver.getGraphDb();
try (Transaction tx = graphDb.beginTx()) {
try (final Transaction tx = graphDb.beginTx()) {
// (sw:Switch)
final Iterable<Node> sws = () -> graphDb.findNodes(Neo4jConstants.labelSwitch);
for (final Node sw : sws) {
Expand Down
Expand Up @@ -31,7 +31,9 @@ public void activate(final Collection<Neo4jSemaphoreNeighborInjectMatch> matches
for (final Neo4jSemaphoreNeighborInjectMatch match : matches) {
final Iterable<Relationship> entries = match.getRoute().getRelationships(Neo4jConstants.relationshipTypeEntry);
for (final Relationship entry : entries) {
entry.delete();
if (entry.getEndNode().equals(match.getSemaphore())) {
entry.delete();
}
}
}
}
Expand Down
Expand Up @@ -28,10 +28,12 @@ public Neo4jCoreTransformationInjectSwitchMonitored(final Neo4jDriver driver) {

@Override
public void activate(final Collection<Neo4jSwitchMonitoredInjectMatch> matches) {
System.out.println(matches.size());
for (final Neo4jSwitchMonitoredInjectMatch match : matches) {
final Iterable<Relationship> sensors = match.getSw().getRelationships(Neo4jConstants.relationshipTypeMonitoredBy);
for (final Relationship sensor : sensors) {
sensor.delete();
System.out.println(match);
final Iterable<Relationship> monitoredBys = match.getSw().getRelationships(Neo4jConstants.relationshipTypeMonitoredBy);
for (final Relationship monitoredBy : monitoredBys) {
monitoredBy.delete();
}
}
}
Expand Down
Expand Up @@ -23,17 +23,20 @@
import hu.bme.mit.trainbenchmark.constants.QueryConstants;
import hu.bme.mit.trainbenchmark.constants.RailwayOperation;

public class Neo4jCypherTransformationInjectSemaphoreNeighbor extends Neo4jCypherTransformation<Neo4jSemaphoreNeighborInjectMatch> {
public class Neo4jCypherTransformationInjectSemaphoreNeighbor
extends Neo4jCypherTransformation<Neo4jSemaphoreNeighborInjectMatch> {

public Neo4jCypherTransformationInjectSemaphoreNeighbor(final Neo4jDriver driver, final String workspaceDir) throws IOException {
public Neo4jCypherTransformationInjectSemaphoreNeighbor(final Neo4jDriver driver, final String workspaceDir)
throws IOException {
super(driver, workspaceDir, RailwayOperation.SEMAPHORENEIGHBOR_INJECT);
}

@Override
public void activate(final Collection<Neo4jSemaphoreNeighborInjectMatch> matches) throws IOException {
for (final Neo4jSemaphoreNeighborInjectMatch match : matches) {
final Map<String, Object> parameters = ImmutableMap.of( //
QueryConstants.VAR_ROUTE, match.getRoute().getId() //
QueryConstants.VAR_ROUTE, match.getRoute().getId(), //
QueryConstants.VAR_SEMAPHORE, match.getSemaphore().getId() //
);

driver.runTransformation(transformationDefinition, parameters);
Expand Down
Expand Up @@ -23,15 +23,19 @@
import hu.bme.mit.trainbenchmark.constants.QueryConstants;
import hu.bme.mit.trainbenchmark.constants.RailwayOperation;

public class Neo4jCypherTransformationInjectSwitchMonitored extends Neo4jCypherTransformation<Neo4jSwitchMonitoredInjectMatch> {
public class Neo4jCypherTransformationInjectSwitchMonitored
extends Neo4jCypherTransformation<Neo4jSwitchMonitoredInjectMatch> {

public Neo4jCypherTransformationInjectSwitchMonitored(final Neo4jDriver driver, final String workspaceDir) throws IOException {
public Neo4jCypherTransformationInjectSwitchMonitored(final Neo4jDriver driver, final String workspaceDir)
throws IOException {
super(driver, workspaceDir, RailwayOperation.SWITCHMONITORED_INJECT);
}

@Override
public void activate(final Collection<Neo4jSwitchMonitoredInjectMatch> matches) throws IOException {
System.out.println(matches.size());
for (final Neo4jSwitchMonitoredInjectMatch match : matches) {
System.out.println(match);
final Map<String, Object> parameters = ImmutableMap.of( //
QueryConstants.VAR_SW, match.getSw().getId() //
);
Expand Down
@@ -1,2 +1,2 @@
MATCH (route:Route)
RETURN DISTINCT route
MATCH (route:Route)-[:entry]->(semaphore:Semaphore)
RETURN DISTINCT route, semaphore
@@ -1,3 +1,4 @@
MATCH (route)-[e:entry]->(semaphore:Semaphore)
WHERE id(route) = { route }
MATCH (route)-[e:entry]->(semaphore)
WHERE id(route) = { route }
AND id(semaphore) = { semaphore }
DELETE e
@@ -1,3 +1,3 @@
MATCH (sw)-[m:monitoredBy]->(sensor:Sensor)
MATCH (sw)-[m:monitoredBy]->(:Sensor)
WHERE id(sw) = { sw }
DELETE m
Expand Up @@ -30,7 +30,7 @@ public RdfDriver(final boolean inferencing) {
this.inferencing = inferencing;
}

protected Long determineNewVertexId() throws Exception {
protected Long determineInitialNewVertexId() throws Exception {
Long id = 5000L;

// safety measure to avoid infinite loop in case of a driver bug
Expand All @@ -46,13 +46,14 @@ protected Long determineNewVertexId() throws Exception {
if (iterationCount > 20) {
throw new IOException("Could not generate new unique id.");
}
System.out.println(id);

return id;
}

public Long getNewVertexId() throws Exception {
public Long generateNewVertexId() throws Exception {
if (newVertexId == null) {
newVertexId = determineNewVertexId();
newVertexId = determineInitialNewVertexId();
}
newVertexId++;
return newVertexId;
Expand Down
@@ -1,7 +1,9 @@
PREFIX base: <http://www.semanticweb.org/ontologies/2015/trainbenchmark#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT ?route
SELECT DISTINCT ?route ?semaphore
WHERE {
?route rdf:type base:Route .
?route base:entry ?semaphore .
?semaphore rdf:type base:Semaphore .
}
Expand Up @@ -12,6 +12,7 @@
package hu.bme.mit.trainbenchmark.benchmark.rdf4j.matches;

import static hu.bme.mit.trainbenchmark.constants.QueryConstants.VAR_ROUTE;
import static hu.bme.mit.trainbenchmark.constants.QueryConstants.VAR_SEMAPHORE;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.query.BindingSet;
Expand All @@ -29,4 +30,9 @@ public IRI getRoute() {
return (IRI) bs.getValue(VAR_ROUTE);
}

@Override
public IRI getSemaphore() {
return (IRI) bs.getValue(VAR_SEMAPHORE);
}

}
Expand Up @@ -50,7 +50,7 @@ public void activate(final Collection<Rdf4jConnectedSegmentsInjectMatch> matches

for (final Rdf4jConnectedSegmentsInjectMatch csim : matches) {
// create (segment2) node
final Long newVertexId = driver.getNewVertexId();
final Long newVertexId = driver.generateNewVertexId();
final IRI segment2 = vf.createIRI(BASE_PREFIX + ID_PREFIX + newVertexId);
connection.add(segment2, RDF.TYPE, segmentType);

Expand Down

0 comments on commit af2b6f3

Please sign in to comment.