That won't work. getEdges returns a bag of Edge not Node objects. You
have to inspect each Edge looking for the neighbor agentB.
I made the same mistake in my first prototype, hoping I could chain calls
together in one line...
On Thu, Sep 26, 2013 at 12:05 PM, Joey Harrison <[log in to unmask]> wrote:
> Bag has a contains method, so you can just do this:
>
> network.getEdges(agentA, null).contains(agentB)
>
> Joey
>
>
> On Thu, Sep 26, 2013 at 12:07 PM, Tony Bigbee <[log in to unmask]>wrote:
>
>> I couldn't find a method in sim.field.network.Network like
>> areNeighbors(Object o1, Object o2).
>>
>> So a starting point would be to use Network's getEdges method and iterate
>> through the returned Bag looking for the other agent node in the edges.
>> You could write a thin wrapper method to do the job and you would have a
>> reusable component, like:
>>
>> public boolean areNeighbors(Network network, Object agentA, Object
>> agentB) {
>> Bag edges = network.getEdges(agentA, null); //obtain all edges that
>> have agentA; the bag may be empty but it won't be null
>> Iterator<Edge> iter = edges.iterator();
>> while (iter.hasNext()) { //iterate
>> through all edges until we find agentB
>> Edge e = iter.next();
>> if (e.getOtherNode(agentA)==agentB) { //use agentA as one
>> node and see if the other node is agentB
>> return true; //found
>> it
>> }
>> }
>> return false; //did not find it
>> }
>>
>> Then to achieve what you originally asked:
>>
>> if ( !areNeighbors(mynetwork, agentA,agentB)) {
>> mynetwork.addEdge(agentA,agentB,null);
>> }
>>
>> Tony
>>
>>
>>
>> On Thu, Sep 26, 2013 at 3:54 AM, Beth Kar <[log in to unmask]> wrote:
>>
>>> Hello!
>>> I am a newbie to mason (and java) and I am trying to dynamically create
>>> an
>>> undirected network. I want to add an edge between two nodes using the
>>> following code:
>>>
>>> mynetwork.addEdge(AgentA, AgentB, null).
>>>
>>> How can I check with an if-then statement before hand if that edge exists
>>> before adding it?
>>>
>>> p.s. If ( edge between AgentA and AgentB doesn't exist) {
>>> mynetwork.addEdge(AgentA, AgentB, null)}
>>>
>>> How do I represent the phrase 'edge between AgentA and AgentB doesn't
>>> exist'
>>> in java?
>>>
>>> Thank you in advance for your help!
>>> Beth.
>>>
>>
>>
>
|