boolean matchNext ( ) { if ( numAtoms == 0 ) return false ; if ( sptr > 1 ) backtrack ( ) ; main : while ( sptr != 0 ) { final int bidx = currBondIdx ( ) ; if ( bidx == numBonds ) { // done if ( numMapped == numAtoms ) return true ; // handle disconnected atoms for ( IAtom qatom : query . atoms ( ) ) { if ( amap [ qatom . getIndex ( ) ] == UNMAPPED ) { Iterator < IAtom > iter = atoms ( ) ; while ( iter . hasNext ( ) ) { IAtom atom = iter . next ( ) ; if ( feasible ( bidx , ( IQueryAtom ) qatom , atom ) ) continue main ; } break ; } } backtrack ( ) ; continue ; } IQueryBond qbond = qbonds [ bidx ] ; IQueryAtom qbeg = ( IQueryAtom ) qbond . getBegin ( ) ; IQueryAtom qend = ( IQueryAtom ) qbond . getEnd ( ) ; int begIdx = amap [ qbeg . getIndex ( ) ] ; int endIdx = amap [ qend . getIndex ( ) ] ; // both atoms matched, there must be a bond between them if ( begIdx != UNMAPPED && endIdx != UNMAPPED ) { IBond bond = mol . getAtom ( begIdx ) . getBond ( mol . getAtom ( endIdx ) ) ; if ( feasible ( qbond , bond ) ) continue ; } // 'beg' is mapped, find a feasible 'end' from it's neighbor list else if ( begIdx != UNMAPPED ) { IAtom beg = mol . getAtom ( begIdx ) ; Iterator < IBond > biter = bonds ( beg ) ; while ( biter . hasNext ( ) ) { IBond bond = biter . next ( ) ; IAtom end = bond . getOther ( beg ) ; if ( qbond . matches ( bond ) && feasible ( bidx + 1 , qend , end ) ) continue main ; } } // 'end' is mapped, find a feasible 'beg' from it's neighbor list else if ( endIdx != UNMAPPED ) { IAtom end = mol . getAtom ( endIdx ) ; Iterator < IBond > biter = bonds ( end ) ; while ( biter . hasNext ( ) ) { IBond bond = biter . next ( ) ; IAtom beg = bond . getOther ( end ) ; if ( qbond . matches ( bond ) && feasible ( bidx + 1 , qbeg , beg ) ) continue main ; } } // 'beg' nor 'end' matched, find a feasible mapping from // any atom in the molecule else { Iterator < IAtom > aiter = atoms ( ) ; while ( aiter . hasNext ( ) ) { if ( feasible ( bidx , qbeg , aiter . next ( ) ) ) continue main ; } } backtrack ( ) ; } return false ; }
Primary match function if this function returns true the algorithm has found a match . Calling it again will backtrack and find the next match .