Skip to content

Commit

Permalink
reintroduce graph generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaillandier committed Jun 29, 2021
1 parent 4b1660e commit e9504f3
Show file tree
Hide file tree
Showing 13 changed files with 529 additions and 249 deletions.
1 change: 0 additions & 1 deletion msi.gama.core/src/msi/gama/util/file/GamaGraphDimacs.java
Expand Up @@ -15,7 +15,6 @@
import msi.gama.precompiler.IConcept;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.graph.IGraph;
import msi.gaml.species.ISpecies;
import msi.gaml.types.IType;

Expand Down
1 change: 0 additions & 1 deletion msi.gama.core/src/msi/gama/util/file/GamaGraphDotFile.java
Expand Up @@ -15,7 +15,6 @@
import msi.gama.precompiler.IConcept;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.graph.IGraph;
import msi.gaml.species.ISpecies;
import msi.gaml.types.IType;

Expand Down
55 changes: 1 addition & 54 deletions msi.gama.core/src/msi/gama/util/file/GamaGraphFile.java
Expand Up @@ -11,26 +11,17 @@
package msi.gama.util.file;


import java.util.HashMap;
import java.util.Map;

import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DirectedMultigraph;
import org.jgrapht.nio.GraphImporter;
import org.jgrapht.util.SupplierUtil;

import msi.gama.common.geometry.Envelope3D;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.metamodel.topology.graph.GamaSpatialGraph;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaListFactory;
import msi.gama.util.GamaMapFactory;
import msi.gama.util.IList;
import msi.gama.util.graph.GamaGraph;
import msi.gama.util.graph.IGraph;
import msi.gama.util.graph.loader.GraphParsers;
import msi.gaml.operators.Spatial;
import msi.gaml.species.ISpecies;
import msi.gaml.types.IContainerType;
import msi.gaml.types.Types;
Expand All @@ -47,14 +38,12 @@ public GamaGraphFile(final IScope scope, final String pn) throws GamaRuntimeExce
public GamaGraphFile(final IScope scope, final String pathName, final ISpecies nodeSpecies) {
super(scope, pathName);
nodeS = nodeSpecies;
System.out.println("ldldldl");
}

public GamaGraphFile(final IScope scope, final String pathName,final ISpecies nodeSpecies, final ISpecies edgeSpecies) {
super(scope, pathName);
nodeS = nodeSpecies;
edgeS = edgeSpecies;
System.out.println("dldldldl");
}

@Override
Expand All @@ -65,54 +54,12 @@ public Envelope3D computeEnvelope(final IScope scope) {
@Override
protected void fillBuffer(final IScope scope) throws GamaRuntimeException {
if (getBuffer() != null) { return; }
System.out.println("lalalala");
GraphImporter parser = GraphParsers.getGraphImporter(getFileType());
DirectedMultigraph<String, DefaultEdge> graph = new DirectedMultigraph<>(
SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, true);

parser.importGraph(graph, this.getFile(scope));
GamaGraph<Object, Object> g = new GamaGraph(scope,nodeS == null ?Types.STRING : Types.AGENT,edgeS == null ?Types.STRING : Types.AGENT);
Map<String, IAgent> verticesAg = new HashMap();
for (Object v : graph.vertexSet()) {
if (nodeS == null)
g.addVertex(v.toString());
else {
IList atts = GamaListFactory.create();
final IList<IAgent> listAgt =(IList<IAgent>) nodeS.getPopulation(scope).createAgents(scope, 1,atts,false, false, null);
IAgent ag = listAgt.get(0);
if (v != null) ag.setName(v.toString());
g.addVertex(ag);
verticesAg.put(v.toString(), ag);
}
}
for (DefaultEdge e : graph.edgeSet()) {
Object s = graph.getEdgeSource(e);
Object t = graph.getEdgeTarget(e);

if (edgeS == null) {
g.addEdge(s, t, e);
g.setEdgeWeight(e, graph.getEdgeWeight(e));
} else {
IList atts = GamaListFactory.create();
final IList<IAgent> listAgt =(IList<IAgent>) edgeS.getPopulation(scope).createAgents(scope, 1,atts,false, true, null);
IAgent ag = listAgt.get(0);
if(e != null) ag.setName(e.toString());

if (nodeS != null) {
IAgent n1 = verticesAg.get(s.toString());
IAgent n2 = verticesAg.get(t.toString());
g.addEdge(n1, n2, ag);
ag.setGeometry(Spatial.Creation.link(scope, n1,n2));
} else
g.addEdge(s, t, ag);

g.setEdgeWeight(ag, graph.getEdgeWeight(e));
}


}

setBuffer(g);
setBuffer(new GamaGraph<>(scope, graph, nodeS, edgeS));
}

abstract protected String getFileType() ;
Expand Down
1 change: 0 additions & 1 deletion msi.gama.core/src/msi/gama/util/file/GamaGraphGML.java
Expand Up @@ -15,7 +15,6 @@
import msi.gama.precompiler.IConcept;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.graph.IGraph;
import msi.gaml.species.ISpecies;
import msi.gaml.types.IType;

Expand Down
1 change: 0 additions & 1 deletion msi.gama.core/src/msi/gama/util/file/GamaGraphGexf.java
Expand Up @@ -15,7 +15,6 @@
import msi.gama.precompiler.IConcept;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.graph.IGraph;
import msi.gaml.species.ISpecies;
import msi.gaml.types.IType;

Expand Down
1 change: 0 additions & 1 deletion msi.gama.core/src/msi/gama/util/file/GamaGraphGraph6.java
Expand Up @@ -15,7 +15,6 @@
import msi.gama.precompiler.IConcept;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.graph.IGraph;
import msi.gaml.species.ISpecies;
import msi.gaml.types.IType;

Expand Down
1 change: 0 additions & 1 deletion msi.gama.core/src/msi/gama/util/file/GamaGraphMLFile.java
Expand Up @@ -15,7 +15,6 @@
import msi.gama.precompiler.IConcept;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.graph.IGraph;
import msi.gaml.species.ISpecies;
import msi.gaml.types.IType;

Expand Down
1 change: 0 additions & 1 deletion msi.gama.core/src/msi/gama/util/file/GamaGraphTsplib.java
Expand Up @@ -15,7 +15,6 @@
import msi.gama.precompiler.IConcept;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.graph.IGraph;
import msi.gaml.species.ISpecies;
import msi.gaml.types.IType;

Expand Down
80 changes: 80 additions & 0 deletions msi.gama.core/src/msi/gama/util/graph/GamaGraph.java
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -35,7 +36,9 @@
import org.jgrapht.alg.tour.HamiltonianCycleAlgorithmBase;
import org.jgrapht.alg.tour.PalmerHamiltonianCycle;
import org.jgrapht.alg.util.Pair;
import org.jgrapht.graph.AbstractBaseGraph;
import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DefaultGraphType;
import org.jgrapht.graph.SimpleWeightedGraph;

Expand Down Expand Up @@ -65,6 +68,7 @@
import msi.gama.util.path.PathFactory;
import msi.gaml.operators.Cast;
import msi.gaml.operators.Graphs.EdgeToAdd;
import msi.gaml.operators.Spatial;
import msi.gaml.operators.Spatial.Creation;
import msi.gaml.operators.Strings;
import msi.gaml.species.ISpecies;
Expand Down Expand Up @@ -146,6 +150,82 @@ public GamaGraph(final IScope scope, final IType nodeType, final IType vertexTyp
this.graphScope = scope;
type = Types.GRAPH.of(nodeType, vertexType);
}

public GamaGraph(IScope scope, AbstractBaseGraph<String, DefaultEdge> graph, ISpecies nodeS, ISpecies edgeS ) {
this(scope,nodeS == null ?Types.STRING : Types.AGENT,edgeS == null ?Types.STRING : Types.AGENT);
Map<String, IAgent> verticesAg = new HashMap();
for (Object v : graph.vertexSet()) {
if (nodeS == null)
addVertex(v.toString());
else {
IList atts = GamaListFactory.create();
final IList<IAgent> listAgt =(IList<IAgent>) nodeS.getPopulation(scope).createAgents(scope, 1,atts,false, false, null);
IAgent ag = listAgt.get(0);
if (v != null) ag.setName(v.toString());
addVertex(ag);
verticesAg.put(v.toString(), ag);
}
}
for (DefaultEdge e : graph.edgeSet()) {
Object s = graph.getEdgeSource(e);
Object t = graph.getEdgeTarget(e);

if (edgeS == null) {
addEdge(s, t, e);
setEdgeWeight(e, graph.getEdgeWeight(e));
} else {
IList atts = GamaListFactory.create();
final IList<IAgent> listAgt =(IList<IAgent>) edgeS.getPopulation(scope).createAgents(scope, 1,atts,false, true, null);
IAgent ag = listAgt.get(0);
if(e != null) ag.setName(e.toString());

if (nodeS != null) {
IAgent n1 = verticesAg.get(s.toString());
IAgent n2 = verticesAg.get(t.toString());
addEdge(n1, n2, ag);
ag.setGeometry(Spatial.Creation.link(scope, n1,n2));
} else
addEdge(s, t, ag);

setEdgeWeight(ag, graph.getEdgeWeight(e));
}

}
}

public GamaGraph(IScope scope, AbstractBaseGraph<String, DefaultEdge> graph, IList nodes, ISpecies edgeS ) {
this(scope, Types.get(nodes.get(0).getClass()) ,edgeS == null ?Types.STRING : Types.AGENT);
Map<String, Object> verticesAg = new HashMap();
for (Object v : graph.vertexSet()) {
Object d = nodes.get(Integer.valueOf(v.toString()));
addVertex(d);
verticesAg.put(v.toString(), d);
}
for (DefaultEdge e : graph.edgeSet()) {
Object s = graph.getEdgeSource(e);
Object t = graph.getEdgeTarget(e);

if (edgeS == null) {
addEdge(s, t, e);
setEdgeWeight(e, graph.getEdgeWeight(e));
} else {
IList atts = GamaListFactory.create();
final IList<IAgent> listAgt =(IList<IAgent>) edgeS.getPopulation(scope).createAgents(scope, 1,atts,false, true, null);
IAgent ag = listAgt.get(0);
if(e != null) ag.setName(e.toString());

Object n1 = verticesAg.get(s.toString());
Object n2 = verticesAg.get(t.toString());
addEdge(n1, n2, ag);
if (n1 instanceof IShape)
ag.setGeometry(Spatial.Creation.link(scope, (IShape)n1,(IShape)n2));

setEdgeWeight(ag, graph.getEdgeWeight(e));
}


}
}

public IScope getScope() {
return graphScope;
Expand Down

0 comments on commit e9504f3

Please sign in to comment.