The advantage of tonys approach is, that it avoids potential unnecessary loops when it finds the neighbour. Your loop iterates always over all edges. Maybe that could make a Difference performance wise... Am 27.09.2013 11:22 schrieb "Beth Kar" <[log in to unmask]>: > Thank you very much for your prompt response. And actually, before > receiving > your replies I kind of figured out a solution. My approach was the similar > to the one that Tony proposed. But instead of using a while loop I did > this: > > Bag edges = mynetwork.getEdges(agentA, null); > int length = edges.size(); > boolean exists = false; > > for (int node=0; node<len; node++){ > Edge e = (Edge)(edges.get(node)); > if(e.getOtherNode(agentA) == agentB){ > exists = true; > }else{continue;} > } > > if (!exists){ > mynetwork.addEdge(agentA, agentB, null); > } > > Thank you again, > Beth > > On Thu, 26 Sep 2013 15:24:26 -0400, Joey Harrison <[log in to unmask]> wrote: > > >Ahh, my mistake. > > > >Joey > > > > > >On Thu, Sep 26, 2013 at 2:38 PM, Tony Bigbee <[log in to unmask]> > wrote: > > > >> 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. > >>>>> > >>>> > >>>> > >>> > >> > > >