Commit adc8d37d authored by Guilhelm Savin's avatar Guilhelm Savin

..

parent 2ac109f8
......@@ -21,7 +21,7 @@
<groupId>org.graphstream</groupId>
<artifactId>gs-boids</artifactId>
<version>git</version>
<version>1.0</version>
<name>gs-boids</name>
<description>
......@@ -42,6 +42,25 @@
<url>https://github.com/graphstream/gs-core/issues</url>
</issueManagement>
<repositories>
<repository>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<layout>default</layout>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
......@@ -189,13 +208,13 @@
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-core</artifactId>
<version>1.2-git</version>
<version>[1.1,)</version>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-algo</artifactId>
<version>1.2-git</version>
<version>[1.1,)</version>
<optional>false</optional>
</dependency>
</dependencies>
......
......@@ -39,6 +39,7 @@ import java.util.HashMap;
import org.graphstream.boids.forces.ntree.NTreeForcesFactory;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.NodeFactory;
import org.graphstream.graph.implementations.AbstractNode;
import org.graphstream.graph.implementations.AdjacencyListGraph;
......@@ -59,6 +60,9 @@ import java.util.Random;
*/
public class BoidGraph extends AdjacencyListGraph {
public static boolean VERBOSE = Boolean.parseBoolean(System.getProperty(
"boids.verbose", "true"));
public static enum Parameter {
MAX_STEPS, AREA, SLEEP_TIME, STORE_FORCES_ATTRIBUTES, NORMALIZE_MODE, RANDOM_SEED, FORCES_FACTORY
}
......@@ -135,11 +139,13 @@ public class BoidGraph extends AdjacencyListGraph {
* Listeners for boid-graph specific events.
*/
protected ArrayList<BoidGraphListener> listeners = new ArrayList<BoidGraphListener>();
/**
* New boids simulation represented as an interaction graph.
*
* <p>All parameters are set to defaults.</p>
* <p>
* All parameters are set to defaults.
* </p>
*/
public BoidGraph() {
super("boids-context");
......@@ -164,10 +170,14 @@ public class BoidGraph extends AdjacencyListGraph {
/**
* New boids simulation represented as an interaction graph.
*
* <p>This pre-load a configuration from a DGS file.</p>
* <p>
* This pre-load a configuration from a DGS file.
* </p>
*
* @param dgsConfig The initial configuration in DGS format.
* @throws IOException If the DGS cannot be read.
* @param dgsConfig
* The initial configuration in DGS format.
* @throws IOException
* If the DGS cannot be read.
*/
public BoidGraph(String dgsConfig) throws IOException {
this();
......@@ -193,6 +203,9 @@ public class BoidGraph extends AdjacencyListGraph {
} catch (FileNotFoundException e) {
in = getClass().getResourceAsStream(dgs);
if (in == null)
in = ClassLoader.getSystemResourceAsStream(dgs);
if (in == null)
throw e;
}
......@@ -219,7 +232,9 @@ public class BoidGraph extends AdjacencyListGraph {
/**
* Set the factory used to instantiate the force system to use.
* @param bff The force system factory.
*
* @param bff
* The force system factory.
*/
public void setForcesFactory(BoidForcesFactory bff) {
if (forcesFactory != null)
......@@ -238,8 +253,9 @@ public class BoidGraph extends AdjacencyListGraph {
forcesFactory.init();
System.out.printf("forces factory is now %s\n", bff.getClass()
.getName());
if (VERBOSE)
System.out.printf("forces factory is now %s\n", bff.getClass()
.getName());
}
public double getArea() {
......@@ -306,6 +322,7 @@ public class BoidGraph extends AdjacencyListGraph {
/**
* The random number generator used.
*
* @return The random number generator.
*/
public Random getRandom() {
......@@ -354,7 +371,9 @@ public class BoidGraph extends AdjacencyListGraph {
}
boidSpecies.put(name, species);
System.out.printf("new species : %s\n", name);
if (VERBOSE)
System.out.printf("new species : %s\n", name);
}
return species;
......@@ -362,7 +381,9 @@ public class BoidGraph extends AdjacencyListGraph {
/**
* The species with the given name.
* @param name The species name.
*
* @param name
* The species name.
* @return The corresponding species or null if not found.
*/
public BoidSpecies getSpecies(String name) {
......@@ -371,6 +392,7 @@ public class BoidGraph extends AdjacencyListGraph {
/**
* The number of boid species actually.
*
* @return The number of boid species.
*/
public int getSpeciesCount() {
......@@ -379,6 +401,7 @@ public class BoidGraph extends AdjacencyListGraph {
/**
* The species whose name is "default".
*
* @return The default species.
*/
public BoidSpecies getDefaultSpecies() {
......@@ -387,7 +410,9 @@ public class BoidGraph extends AdjacencyListGraph {
/**
* Add a species with name "default" if does not yet exists.
* @return The created default species or the old one if it was already present.
*
* @return The created default species or the old one if it was already
* present.
*/
public BoidSpecies addDefaultSpecies() {
return getOrCreateSpecies("default");
......@@ -396,14 +421,16 @@ public class BoidGraph extends AdjacencyListGraph {
/**
* Remove a species.
*
* This also removed all the boids of this species. You cannot remove the "default" species.
* This also removed all the boids of this species. You cannot remove the
* "default" species.
*
* @param name The species name.
* @param name
* The species name.
*/
public void deleteSpecies(String name) {
if (!name.equals("default")) {
BoidSpecies species = boidSpecies.get(name);
if(species != null) {
if (species != null) {
species.release();
boidSpecies.remove(name);
}
......@@ -528,7 +555,7 @@ public class BoidGraph extends AdjacencyListGraph {
}
forcesFactory.step();
super.stepBegins(step);
}
......@@ -561,29 +588,40 @@ public class BoidGraph extends AdjacencyListGraph {
public void addBoidGraphListener(BoidGraphListener listener) {
listeners.add(listener);
}
/**
* Unregister a listener for boid specific events.
* @param listener The listener to remove.
*
* @param listener
* The listener to remove.
*/
public void removeBoidGraphListener(BoidGraphListener listener) {
int index = listeners.indexOf(listener);
if(index >= 0) {
if (index >= 0) {
listeners.remove(index);
}
}
@Override
protected void addNodeCallback(AbstractNode node) {
Boid b = (Boid) node;
public <T extends Node> T addNode(String nodeId) {
T n = super.addNode(nodeId);
Boid b = (Boid) n;
super.addNodeCallback(node);
AbstractNode n = getNode(b.getId());
b.getSpecies().register(b);
b.getSpecies().checkClasses(b);
for (BoidGraphListener listener : listeners) {
listener.boidAdded(b);
}
return n;
}
@Override
protected void addNodeCallback(AbstractNode node) {
Boid b = (Boid) node;
super.addNodeCallback(node);
b.getSpecies().register(b);
}
@Override
......@@ -637,8 +675,10 @@ public class BoidGraph extends AdjacencyListGraph {
if (key != null) {
try {
species.set(key, newValue == null ? null : newValue
.toString());
species.set(
key,
newValue == null ? null : newValue
.toString());
} catch (IllegalArgumentException e) {
System.err.printf("(WW) invalid parameter '%s'\n",
key);
......
......@@ -137,7 +137,7 @@ public class BoidSpecies implements Iterable<Boid> {
protected HashMap<String, Boid> boids;
/**
* Specify a CSS class for the species name for the GraphStream viewer.
* Specify a CSS class for the species name for the GraphStream viewer.
*/
protected boolean addSpeciesNameInUIClass;
......@@ -145,16 +145,16 @@ public class BoidSpecies implements Iterable<Boid> {
* Allow to create unique identifiers for boids.
*/
protected int currentIndex = 0;
/**
* Allow to create unique identifiers for boids.
*/
protected long timestamp = System.nanoTime();
/**
* Handle the addition or removal of boids.
*/
protected DemographicManager pop;
protected DemographicManager pop;
/**
* New default species with a random color.
......@@ -183,7 +183,9 @@ public class BoidSpecies implements Iterable<Boid> {
inertia = 1.1f;
fearFactor = 1;
addSpeciesNameInUIClass = true;
pop = new DemographicManager.SpeciesDemographicManager(this, ctx, new Probability.ConstantProbability(0.01), new Probability.ConstantProbability(0.01));
pop = new DemographicManager.SpeciesDemographicManager(this, ctx,
new Probability.ConstantProbability(0.01),
new Probability.ConstantProbability(0.01));
this.color = new Color(ctx.random.nextFloat(), ctx.random.nextFloat(),
ctx.random.nextFloat());
......@@ -226,7 +228,8 @@ public class BoidSpecies implements Iterable<Boid> {
* string value of the parameter
*/
public void set(Parameter p, String val) {
System.out.printf("set %s of %s to %s\n", p.name(), name, val);
if (BoidGraph.VERBOSE)
System.out.printf("set %s of %s to %s\n", p.name(), name, val);
switch (p) {
case COUNT:
......@@ -301,7 +304,9 @@ public class BoidSpecies implements Iterable<Boid> {
void register(Boid b) {
boids.put(b.getId(), b);
}
void checkClasses(Boid b) {
if (addSpeciesNameInUIClass) {
String uiClass = b.getAttribute("ui.class");
......@@ -317,24 +322,25 @@ public class BoidSpecies implements Iterable<Boid> {
void unregister(Boid b) {
boids.remove(b.getId());
if (addSpeciesNameInUIClass) {
String uiClass = b.getAttribute("ui.class");
if (uiClass != null && uiClass.indexOf(name) != -1) {
uiClass = uiClass.replaceAll("(^|\\s)" + name + "($|\\s)", " ");
uiClass = uiClass.trim();
if(uiClass.length() == 0 )
b.removeAttribute("ui.class");
else b.setAttribute("ui.class", uiClass);
}
}
/*
* if (addSpeciesNameInUIClass) { String uiClass =
* b.getAttribute("ui.class");
*
* if (uiClass != null && uiClass.indexOf(name) != -1) { uiClass =
* uiClass.replaceAll("(^|\\s)" + name + "($|\\s)", " "); uiClass =
* uiClass.trim();
*
* if(uiClass.length() == 0 ) b.removeAttribute("ui.class"); else
* b.setAttribute("ui.class", uiClass); } }
*/
}
/**
* This method is called by {@link org.graphstream.boids.BoidGraph} at the
* end of each step. It can be used by sub-classes to add some code.
* @param time The current boid graph time.
*
* @param time
* The current boid graph time.
*/
public void terminateStep(double time) {
pop.step(time);
......@@ -577,41 +583,46 @@ public class BoidSpecies implements Iterable<Boid> {
public void setAngleOfView(double aov) {
angleOfView = aov;
}
/**
* The probability that a boids clones itself.
* @param probability A probability instance.
*
* @param probability
* A probability instance.
*/
public void setReproductionProbability(Probability probability) {
pop.setReproduceCondition(probability);
}
/**
* The probability that a boids die.
* @param probability A probability instance.
*
* @param probability
* A probability instance.
*/
public void setDeathCondition(Probability probability) {
pop.setDeathProbability(probability);
}
/**
* Unregister this species from the boids graph.
*
* This removes all boids pertaining to this species, and release the link with the graph.
* This removes all boids pertaining to this species, and release the link
* with the graph.
*/
public void release() {
pop.release();
Iterator<Node> i = ctx.getNodeIterator();
while(i.hasNext()) {
while (i.hasNext()) {
Boid b = (Boid) i.next();
if(b.getSpecies() == this) {
if (b.getSpecies() == this) {
i.remove();
}
}
}
/*
* (non-Javadoc)
*
......
......@@ -53,7 +53,7 @@ cg boids.species.bee.add_species_name_in_ui_class=true
# This last command create 100 boids of the moustik species
#
cg boids.species.moustik.count=80
cg boids.species.bee.count=40
cg boids.species.moustik.count=100
cg boids.species.bee.count=100
cg boids.forces_factory=org.graphstream.boids.forces.ntree.NTreeForcesFactory
cg boids.forces_factory=org.graphstream.boids.forces.greedy.GreedyForcesFactory
......@@ -35,7 +35,7 @@ import org.graphstream.boids.BoidGraph;
import org.miv.pherd.geom.Point3;
public class GreedyForcesFactory implements BoidForcesFactory {
BoidGraph ctx;
protected BoidGraph ctx;
public GreedyForcesFactory(BoidGraph ctx) {
this.ctx = ctx;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment