Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[traffic] Avoid duplicating info in road nodes
- Loading branch information
1 parent
479b668
commit 6d60da6
Showing
4 changed files
with
69 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...ls.gaml.extensions.traffic/src/simtools/gaml/extensions/traffic/driving/DrivingGraph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters