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

Add the 'add/remove graph attribute' commands and their tests

parent f57d483f
...@@ -72,6 +72,24 @@ global { ...@@ -72,6 +72,24 @@ global {
// Step // Step
gs_step gs_sender_id:"test1" gs_step_number:3; gs_step gs_sender_id:"test1" gs_step_number:3;
// Add attributes on node
// A string attribute
gs_add_graph_attribute gs_sender_id:"test1" gs_attribute_name:"string" gs_attribute_value:"a string value";
// A double attribute
gs_add_graph_attribute gs_sender_id:"test1" gs_attribute_name:"double" gs_attribute_value:10.0;
// An integer attribute
gs_add_graph_attribute gs_sender_id:"test1" gs_attribute_name:"integer" gs_attribute_value:1;
// A boolean attribute
gs_add_graph_attribute gs_sender_id:"test1" gs_attribute_name:"boolean" gs_attribute_value:true;
// A list attribute
gs_add_graph_attribute gs_sender_id:"test1" gs_attribute_name:"list" gs_attribute_value:[1,2,3];
// Remove graph attribute
gs_remove_graph_attribute gs_sender_id:"test1" gs_attribute_name:"string";
// Step
gs_step gs_sender_id:"test1" gs_step_number:4;
// Clear // Clear
//gs_clear gs_sender_id:"test1"; //gs_clear gs_sender_id:"test1";
......
...@@ -21,34 +21,49 @@ public class SimpleSinkAdapter extends SinkAdapter { ...@@ -21,34 +21,49 @@ public class SimpleSinkAdapter extends SinkAdapter {
public void stepBegins(String sourceId, long timeId, double step) { public void stepBegins(String sourceId, long timeId, double step) {
System.out.println("Edges attributes :"); System.out.println("Edges attributes :");
for(Edge e : graph.getEachEdge()){ for(Edge e : graph.getEachEdge()){
for(Object o : e.getAttributeKeySet()){ for(String o : e.getAttributeKeySet()){
System.out.println(e.getAttribute((String)o).getClass()); System.out.println(e.getAttribute(o).getClass());
if(e.getAttribute((String)o).getClass().isArray()){ if(e.getAttribute(o).getClass().isArray()){
Object[] array = (Object[])e.getAttribute((String)o); Object[] array = (Object[])e.getAttribute(o);
System.out.print('\t'); System.out.print('\t');
for(Object ob : array) for(Object ob : array)
System.out.print(ob); System.out.print(ob);
System.out.println(); System.out.println();
} }
else{ else{
System.out.println("\tvalue = "+e.getAttribute((String)o)); System.out.println("\tvalue = "+e.getAttribute(o));
} }
} }
} }
System.out.println("Nodes attributes :"); System.out.println("Nodes attributes :");
for(Node e : graph.getEachNode()){ for(Node e : graph.getEachNode()){
for(Object o : e.getAttributeKeySet()){ for(String o : e.getAttributeKeySet()){
System.out.println(e.getAttribute((String)o).getClass()); System.out.println(e.getAttribute(o).getClass());
if(e.getAttribute((String)o).getClass().isArray()){ if(e.getAttribute(o).getClass().isArray()){
Object[] array = (Object[])e.getAttribute((String)o); Object[] array = (Object[])e.getAttribute(o);
System.out.print('\t'); System.out.print('\t');
for(Object ob : array) for(Object ob : array)
System.out.print(ob); System.out.print(ob);
System.out.println(); System.out.println();
} }
else{ else{
System.out.println("\tvalue = "+e.getAttribute((String)o)); System.out.println("\tvalue = "+e.getAttribute(o));
} } }
}
}
System.out.println("Graph attributes :");
for(String o : graph.getAttributeKeySet()){
System.out.println(graph.getAttribute(o).getClass());
if(graph.getAttribute(o).getClass().isArray()){
Object[] array = (Object[])graph.getAttribute(o);
System.out.print('\t');
for(Object ob : array)
System.out.print(ob);
System.out.println();
}
else{
System.out.println("\tvalue = "+graph.getAttribute(o));
}
} }
} }
......
...@@ -7,6 +7,7 @@ public class IKeywordGSAdditional implements IKeyword { ...@@ -7,6 +7,7 @@ public class IKeywordGSAdditional implements IKeyword {
public static final String ADD_EDGE = "gs_add_edge"; public static final String ADD_EDGE = "gs_add_edge";
public static final String ADD_EDGE_ATTRIBUTE = "gs_add_edge_attribute"; public static final String ADD_EDGE_ATTRIBUTE = "gs_add_edge_attribute";
public static final String ADD_GRAPH_ATTRIBUTE = "gs_add_graph_attribute";
public static final String ADD_NODE = "gs_add_node"; public static final String ADD_NODE = "gs_add_node";
public static final String ADD_NODE_ATTRIBUTE = "gs_add_node_attribute"; public static final String ADD_NODE_ATTRIBUTE = "gs_add_node_attribute";
public static final String ADD_SENDER = "gs_add_sender"; public static final String ADD_SENDER = "gs_add_sender";
...@@ -15,6 +16,7 @@ public class IKeywordGSAdditional implements IKeyword { ...@@ -15,6 +16,7 @@ public class IKeywordGSAdditional implements IKeyword {
public static final String CLOSE = "gs_close"; public static final String CLOSE = "gs_close";
public static final String REMOVE_EDGE = "gs_remove_edge"; public static final String REMOVE_EDGE = "gs_remove_edge";
public static final String REMOVE_EDGE_ATTRIBUTE = "gs_remove_edge_attribute"; public static final String REMOVE_EDGE_ATTRIBUTE = "gs_remove_edge_attribute";
public static final String REMOVE_GRAPH_ATTRIBUTE = "gs_remove_graph_attribute";
public static final String REMOVE_NODE = "gs_remove_node"; public static final String REMOVE_NODE = "gs_remove_node";
public static final String REMOVE_NODE_ATTRIBUTE = "gs_remove_node_attribute"; public static final String REMOVE_NODE_ATTRIBUTE = "gs_remove_node_attribute";
public static final String STEP = "gs_step"; public static final String STEP = "gs_step";
......
package org.graphstream.gama.extension.sender;
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.SENDERID, type = IType.STRING, optional = false),
@facet(name = IKeywordGSAdditional.ATTRIBUTE_NAME, type = IType.STRING, optional = false),
@facet(name = IKeywordGSAdditional.ATTRIBUTE_VALUE, type = IType.NONE, optional = false)})
public class AddGraphAttributeStatement extends AbstractStatement implements IStatement{
final IExpression senderid;
final IExpression attname;
final IExpression attval;
public AddGraphAttributeStatement(IDescription desc) {
super(desc);
senderid = getFacet(IKeywordGSAdditional.SENDERID);
attname = getFacet(IKeywordGSAdditional.ATTRIBUTE_NAME);
attval = getFacet(IKeywordGSAdditional.ATTRIBUTE_VALUE);
}
@Override
protected Object privateExecuteIn(IScope scope) throws GamaRuntimeException {
String s = (String)(senderid.value(scope));
String an = (String)(attname.value(scope));
Object av = attval.value(scope);
GSSender sender = GSManager.getSender(s);
// If it is a GamaList, it must be cast to an array
if(av instanceof msi.gama.util.GamaList){
Object[] av_ar = ((msi.gama.util.GamaList) av).toArray();
sender.sendGraphAttributeAdded(an, av_ar);
}
else {
sender.sendGraphAttributeAdded(an, av);
}
return null;
}
}
\ No newline at end of file
package org.graphstream.gama.extension.sender;
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.REMOVE_GRAPH_ATTRIBUTE, kind = ISymbolKind.SINGLE_STATEMENT, with_sequence = false)
@inside(kinds = { ISymbolKind.BEHAVIOR, ISymbolKind.SINGLE_STATEMENT })
@facets(value = { @facet(name = IKeywordGSAdditional.SENDERID, type = IType.STRING, optional = false),
@facet(name = IKeywordGSAdditional.ATTRIBUTE_NAME, type = IType.STRING, optional = false)})
public class RemoveGraphAttributeStatement extends AbstractStatement implements IStatement{
final IExpression senderid;
final IExpression attname;
public RemoveGraphAttributeStatement(IDescription desc) {
super(desc);
senderid = getFacet(IKeywordGSAdditional.SENDERID);
attname = getFacet(IKeywordGSAdditional.ATTRIBUTE_NAME);
}
@Override
protected Object privateExecuteIn(IScope scope) throws GamaRuntimeException {
String s = (String)(senderid.value(scope));
String an = (String)(attname.value(scope));
GSSender sender = GSManager.getSender(s);
sender.sendGraphAttributeRemoved(an);
return null;
}
}
\ 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