Skip to content

Commit

Permalink
[traffic] Avoid duplicating info in road nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc0711 committed Dec 8, 2021
1 parent 479b668 commit 6d60da6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
Expand Up @@ -291,16 +291,6 @@ public boolean addEdgeWithNodes(final IScope scope, final IShape e, final IMap<G
final IShape v2 = nodes.get(ptT);
if (v2 == null) return false;

if (e instanceof IAgent && ((IAgent) e).getSpecies().implementsSkill("skill_road")) {
final IShape ag = e.getAgent();
final List v1ro = (List) v1.getAttribute("roads_out");
v1ro.add(ag);
final List v2ri = (List) v2.getAttribute("roads_in");
v2ri.add(ag);
ag.setAttribute("source_node", v1);
ag.setAttribute("target_node", v2);
}

addVertex(v1);
addVertex(v2);
_Edge<IShape, IShape> edge;
Expand Down
@@ -0,0 +1,67 @@
package simtools.gaml.extensions.traffic.driving;

import java.util.List;

import org.locationtech.jts.geom.Coordinate;

import msi.gama.common.util.StringUtils;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.metamodel.shape.IShape;
import msi.gama.metamodel.topology.graph.GamaSpatialGraph;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.IContainer;
import msi.gama.util.IMap;
import msi.gama.util.graph.GraphEvent;
import msi.gama.util.graph.GraphEvent.GraphEventType;
import msi.gama.util.graph._Edge;

public class DrivingGraph extends GamaSpatialGraph {
public DrivingGraph(final IContainer edges, final IContainer vertices, final IScope scope) {
super(scope, vertices.getGamlType().getContentType(), edges.getGamlType().getContentType());
init(scope, edges, vertices);
}

@Override
public boolean addEdgeWithNodes(final IScope scope, final IShape e, final IMap<GamaPoint, IShape> nodes) {
if (containsEdge(e)) return false;
final Coordinate[] coord = e.getInnerGeometry().getCoordinates();
final IShape ptS = new GamaPoint(coord[0]);
final IShape ptT = new GamaPoint(coord[coord.length - 1]);
final IShape v1 = nodes.get(ptS);
if (v1 == null) return false;
final IShape v2 = nodes.get(ptT);
if (v2 == null) return false;

if (e instanceof IAgent && ((IAgent) e).getSpecies().implementsSkill("skill_road")) {
final IAgent roadAgent = e.getAgent();
final IAgent source = v1.getAgent();
final IAgent target = v2.getAgent();
final List<IAgent> v1ro = RoadNodeSkill.getRoadsOut(source);
if (!v1ro.contains(roadAgent)) {
v1ro.add(roadAgent);
}
final List<IAgent> v2ri = RoadNodeSkill.getRoadsIn(target);
if (!v2ri.contains(roadAgent)) {
v2ri.add(roadAgent);
}
RoadSkill.setSourceNode(roadAgent, source);
RoadSkill.setTargetNode(roadAgent, target);
}

addVertex(v1);
addVertex(v2);
_Edge<IShape, IShape> edge;
try {
edge = newEdge(e, v1, v2);
} catch (final GamaRuntimeException e1) {
e1.addContext("Impossible to create edge from " + StringUtils.toGaml(e, false) + " in graph " + this);
throw e1;
}
// if ( edge == null ) { return false; }
edgeMap.put(e, edge);
dispatchEvent(scope, new GraphEvent(scope, this, this, e, null, GraphEventType.EDGE_ADDED));
return true;
}
}
Expand Up @@ -44,6 +44,6 @@ public class Operators {
)
@no_test
public static IGraph spatialDrivingFromEdges(final IScope scope, final IContainer edges, final IContainer nodes) {
return new GamaSpatialGraph(edges, nodes, scope);
return new DrivingGraph(edges, nodes, scope);
}
}
Expand Up @@ -164,7 +164,7 @@ public static IAgent getTargetNode(final IAgent agent) {
}

@setter(TARGET_NODE)
public void setTargetNode(final IAgent agent, final IAgent nd) {
public static void setTargetNode(final IAgent agent, final IAgent nd) {
agent.setAttribute(TARGET_NODE, nd);
}

Expand Down

0 comments on commit 6d60da6

Please sign in to comment.