Commit d9ce2b19 authored by Thibaut Démare's avatar Thibaut Démare

Add stuff about receiver and the 'get edge/node/graph attribute' commands

parent de2f888a
package org.graphstream.gama.extension.receiver;
import java.util.HashMap;
import java.util.Map;
import msi.gama.util.GamaList;
/**
* Helper class.
*
* Stocks list of values for each attribute. Provides methods to add a value to
* a list and to retrieve a list from attribute name. Handles the conversion
* from NetStream types to NetLogo types.
*
* This class is almost a copy of the one from gs-netlogo but updated to run with Gama
*
* @author Stefan Balev, modified by Thibaut Dmare
*
*/
public class Attributes {
Map<String, GamaList> map;
public Attributes() {
map = new HashMap<String, GamaList>();
}
public GamaList get(String attribute) {
return map.remove(attribute);
}
public void add(String attribute, Object value) {
Object gamaValue = netStreamToGama(value);
if (gamaValue == null)
return;
GamaList list = map.get(attribute);
if (list == null) {
list = new GamaList();
map.put(attribute, list);
}
list.add(gamaValue);
}
protected static Object netStreamToGama(Object o) {
Object result = simpleNetStreamToGama(o);
if (result != null)
return result;
if (!o.getClass().isArray())
return null;
GamaList list = new GamaList();
for (Object element : (Object[]) o) {
Object gamaElement = simpleNetStreamToGama(element);
if (gamaElement == null)
return null;
list.add(gamaElement);
}
return list;
}
protected static Object simpleNetStreamToGama(Object o) {
if (o instanceof Boolean || o instanceof String || o instanceof Double)
return o;
if (o instanceof Number) {
return new Double(((Number) o).doubleValue());
}
return null;
}
}
\ No newline at end of file
package org.graphstream.gama.extension.receiver;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaList;
import org.graphstream.stream.SinkAdapter;
import org.graphstream.stream.netstream.NetStreamReceiver;
import org.graphstream.stream.sync.SinkTime;
import org.graphstream.stream.thread.ThreadProxyPipe;
/**
* A receiver.
*
* A NetStreamReceiver plus helper methods that do the real job for all the
* receiver primitives.
*
* This class is almost a copy of the one from gs-netlogo but updated to run with Gama
*
* @author Stefan Balev, modified by Thibaut Dmare
*
*/
public class GSReceiver extends SinkAdapter {
protected SinkTime sinkTime;
protected NetStreamReceiver nsReceiver;
protected ThreadProxyPipe pipe;
protected Attributes graphAttributes;
protected Map<String, Attributes> nodeAttributes;
protected Map<String, Attributes> edgeAttributes;
protected Queue<Double> steps;
protected Set<String> attributeFilter;
public GSReceiver(SinkTime sinkTime, String host, int port,
Set<String> attributeFilter) throws GamaRuntimeException {
this.sinkTime = sinkTime;
this.attributeFilter = attributeFilter;
try {
nsReceiver = new NetStreamReceiver(host, port);
} catch (UnknownHostException e) {
throw new RuntimeException(e.getMessage());
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
pipe = nsReceiver.getDefaultStream();
pipe.addSink(this);
graphAttributes = new Attributes();
nodeAttributes = new HashMap<String, Attributes>();
edgeAttributes = new HashMap<String, Attributes>();
steps = new LinkedList<Double>();
}
public GamaList receiveGraphAttribute(String attribute) {
pipe.pump();
return graphAttributes.get(attribute);
}
public GamaList receiveNodeAttribute(String nodeId, String attribute) {
pipe.pump();
Attributes a = nodeAttributes.get(nodeId);
return a == null ? null : a.get(attribute);
}
public GamaList receiveEdgeAttribute(String edgeId, String attribute) {
pipe.pump();
Attributes a = edgeAttributes.get(edgeId);
return a == null ? null : a.get(attribute);
}
public Double waitStep() {
while (steps.isEmpty()) {
// In new version of graphstream, we can use the following method :
//pipe.blockingPump();
// But the gs-core used by gama, do not allow us to use it, so we must use the following try/catch
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
pipe.pump();
}
return steps.remove();
}
public void flush() {
pipe.pump();
graphAttributes = new Attributes();
nodeAttributes.clear();
edgeAttributes.clear();
steps.clear();
}
public void close() {
nsReceiver.quit();
}
// Sink methods
@Override
public void edgeAttributeAdded(String sourceId, long timeId, String edgeId,
String attribute, Object value) {
if (sinkTime.isNewEvent(sourceId, timeId)
&& (attributeFilter == null || attributeFilter
.contains(attribute))) {
Attributes a = edgeAttributes.get(edgeId);
if (a == null) {
a = new Attributes();
edgeAttributes.put(edgeId, a);
}
a.add(attribute, value);
}
}
@Override
public void edgeAttributeChanged(String sourceId, long timeId,
String edgeId, String attribute, Object oldValue, Object newValue) {
edgeAttributeAdded(sourceId, timeId, edgeId, attribute, newValue);
}
@Override
public void graphAttributeAdded(String sourceId, long timeId,
String attribute, Object value) {
if (sinkTime.isNewEvent(sourceId, timeId)
&& (attributeFilter == null || attributeFilter
.contains(attribute)))
graphAttributes.add(attribute, value);
}
@Override
public void graphAttributeChanged(String sourceId, long timeId,
String attribute, Object oldValue, Object newValue) {
graphAttributeAdded(sourceId, timeId, attribute, newValue);
}
@Override
public void nodeAttributeAdded(String sourceId, long timeId, String nodeId,
String attribute, Object value) {
if (sinkTime.isNewEvent(sourceId, timeId)
&& (attributeFilter == null || attributeFilter
.contains(attribute))) {
Attributes a = nodeAttributes.get(nodeId);
if (a == null) {
a = new Attributes();
nodeAttributes.put(nodeId, a);
}
a.add(attribute, value);
}
}
@Override
public void nodeAttributeChanged(String sourceId, long timeId,
String nodeId, String attribute, Object oldValue, Object newValue) {
nodeAttributeAdded(sourceId, timeId, nodeId, attribute, newValue);
}
@Override
public void stepBegins(String sourceId, long timeId, double step) {
if (sinkTime.isNewEvent(sourceId, timeId))
steps.add(step);
}
}
\ No newline at end of file
package org.graphstream.gama.extension.receiver;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.precompiler.ISymbolKind;
import msi.gama.precompiler.GamlAnnotations.facet;
import msi.gama.precompiler.GamlAnnotations.facets;
import msi.gama.precompiler.GamlAnnotations.inside;
import msi.gama.precompiler.GamlAnnotations.symbol;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gaml.descriptions.IDescription;
import msi.gaml.expressions.IExpression;
import msi.gaml.statements.AbstractStatement;
import msi.gaml.statements.IStatement;
import msi.gaml.types.IType;
import org.graphstream.gama.extension.GSManager;
import org.graphstream.gama.extension.IKeywordGSAdditional;
@symbol(name = IKeywordGSAdditional.ADD_EDGE_ATTRIBUTE, kind = ISymbolKind.SINGLE_STATEMENT, with_sequence = false)
@inside(kinds = { ISymbolKind.BEHAVIOR, ISymbolKind.SINGLE_STATEMENT })
@facets(value = { @facet(name = IKeywordGSAdditional.RECEIVERID, type = IType.STRING, optional = false),
@facet(name = IKeywordGSAdditional.EDGE_ID, type = IType.STRING, optional = false),
@facet(name = IKeywordGSAdditional.ATTRIBUTE_NAME, type = IType.STRING, optional = false),
@facet(name = IKeyword.RETURNS, type = IType.NEW_TEMP_ID, optional = false)})
public class GetEdgeAttributeStatement extends AbstractStatement implements IStatement{
final IExpression receiverid;
final IExpression edgeid;
final IExpression attname;
final String returns;
public GetEdgeAttributeStatement(IDescription desc) {
super(desc);
receiverid = getFacet(IKeywordGSAdditional.RECEIVERID);
edgeid = getFacet(IKeywordGSAdditional.EDGE_ID);
attname = getFacet(IKeywordGSAdditional.ATTRIBUTE_NAME);
returns = getLiteral(IKeyword.RETURNS);
}
@Override
protected Object privateExecuteIn(IScope scope) throws GamaRuntimeException {
String r = (String)(receiverid.value(scope));
String eid = (String)(edgeid.value(scope));
String an = (String)(attname.value(scope));
GSReceiver receiver = GSManager.getReceiver(r);
Object av = receiver.receiveEdgeAttribute(eid, an);
// and we return the attribute list
scope.setVarValue(returns, av);
return av;
}
}
\ No newline at end of file
package org.graphstream.gama.extension.receiver;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.precompiler.ISymbolKind;
import msi.gama.precompiler.GamlAnnotations.facet;
import msi.gama.precompiler.GamlAnnotations.facets;
import msi.gama.precompiler.GamlAnnotations.inside;
import msi.gama.precompiler.GamlAnnotations.symbol;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gaml.descriptions.IDescription;
import msi.gaml.expressions.IExpression;
import msi.gaml.statements.AbstractStatement;
import msi.gaml.statements.IStatement;
import msi.gaml.types.IType;
import org.graphstream.gama.extension.GSManager;
import org.graphstream.gama.extension.IKeywordGSAdditional;
@symbol(name = IKeywordGSAdditional.ADD_GRAPH_ATTRIBUTE, kind = ISymbolKind.SINGLE_STATEMENT, with_sequence = false)
@inside(kinds = { ISymbolKind.BEHAVIOR, ISymbolKind.SINGLE_STATEMENT })
@facets(value = { @facet(name = IKeywordGSAdditional.RECEIVERID, type = IType.STRING, optional = false),
@facet(name = IKeywordGSAdditional.ATTRIBUTE_NAME, type = IType.STRING, optional = false),
@facet(name = IKeyword.RETURNS, type = IType.NEW_TEMP_ID, optional = false)})
public class GetGraphAttributeStatement extends AbstractStatement implements IStatement{
final IExpression receiverid;
final IExpression attname;
final String returns;
public GetGraphAttributeStatement(IDescription desc) {
super(desc);
receiverid = getFacet(IKeywordGSAdditional.RECEIVERID);
attname = getFacet(IKeywordGSAdditional.ATTRIBUTE_NAME);
returns = getLiteral(IKeyword.RETURNS);
}
@Override
protected Object privateExecuteIn(IScope scope) throws GamaRuntimeException {
String r = (String)(receiverid.value(scope));
String an = (String)(attname.value(scope));
GSReceiver receiver = GSManager.getReceiver(r);
Object av = receiver.receiveGraphAttribute(an);
// and we return the attribute list
scope.setVarValue(returns, av);
return av;
}
}
\ No newline at end of file
package org.graphstream.gama.extension.receiver;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.precompiler.ISymbolKind;
import msi.gama.precompiler.GamlAnnotations.facet;
import msi.gama.precompiler.GamlAnnotations.facets;
import msi.gama.precompiler.GamlAnnotations.inside;
import msi.gama.precompiler.GamlAnnotations.symbol;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gaml.descriptions.IDescription;
import msi.gaml.expressions.IExpression;
import msi.gaml.statements.AbstractStatement;
import msi.gaml.statements.IStatement;
import msi.gaml.types.IType;
import org.graphstream.gama.extension.GSManager;
import org.graphstream.gama.extension.IKeywordGSAdditional;
@symbol(name = IKeywordGSAdditional.ADD_NODE_ATTRIBUTE, kind = ISymbolKind.SINGLE_STATEMENT, with_sequence = false)
@inside(kinds = { ISymbolKind.BEHAVIOR, ISymbolKind.SINGLE_STATEMENT })
@facets(value = { @facet(name = IKeywordGSAdditional.RECEIVERID, type = IType.STRING, optional = false),
@facet(name = IKeywordGSAdditional.NODE_ID, type = IType.STRING, optional = false),
@facet(name = IKeywordGSAdditional.ATTRIBUTE_NAME, type = IType.STRING, optional = false),
@facet(name = IKeyword.RETURNS, type = IType.NEW_TEMP_ID, optional = false)})
public class GetNodeAttributeStatement extends AbstractStatement implements IStatement{
final IExpression receiverid;
final IExpression nodeid;
final IExpression attname;
final String returns;
public GetNodeAttributeStatement(IDescription desc) {
super(desc);
receiverid = getFacet(IKeywordGSAdditional.RECEIVERID);
nodeid = getFacet(IKeywordGSAdditional.NODE_ID);
attname = getFacet(IKeywordGSAdditional.ATTRIBUTE_NAME);
returns = getLiteral(IKeyword.RETURNS);
}
@Override
protected Object privateExecuteIn(IScope scope) throws GamaRuntimeException {
String r = (String)(receiverid.value(scope));
String eid = (String)(nodeid.value(scope));
String an = (String)(attname.value(scope));
GSReceiver receiver = GSManager.getReceiver(r);
Object av = receiver.receiveNodeAttribute(eid, an);
// and we return the attribute list
scope.setVarValue(returns, av);
return av;
}
}
\ No newline at end of file
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