public void findSameAsChaining(Resource subj,Set<Resource> currentSameAs,Resource[] contxts) throws InferenceEngineException {
  try {
    CloseableIteration<Statement,QueryEvaluationException> subjIter=RyaDAOHelper.query(ryaDAO,subj,OWL.SAMEAS,null,conf,contxts);
    while (subjIter.hasNext()) {
      Statement st=subjIter.next();
      if (!currentSameAs.contains(st.getObject())) {
        Resource castedObj=(Resource)st.getObject();
        currentSameAs.add(castedObj);
        findSameAsChaining(castedObj,currentSameAs,contxts);
      }
    }
    subjIter.close();
    CloseableIteration<Statement,QueryEvaluationException> objIter=RyaDAOHelper.query(ryaDAO,null,OWL.SAMEAS,subj,conf,contxts);
    while (objIter.hasNext()) {
      Statement st=objIter.next();
      if (!currentSameAs.contains(st.getSubject())) {
        Resource sameAsSubj=st.getSubject();
        currentSameAs.add(sameAsSubj);
        findSameAsChaining(sameAsSubj,currentSameAs,contxts);
      }
    }
    objIter.close();
  }
 catch (  QueryEvaluationException e) {
    throw new InferenceEngineException(e);
  }
}

public void findSameAsChaining ( Resource subj , Set < Resource > currentSameAs , Resource [ ] contxts ) throws InferenceEngineException { try { CloseableIteration < Statement , QueryEvaluationException > subjIter = RyaDAOHelper . query ( ryaDAO , subj , OWL . SAMEAS , null , conf , contxts ) ; while ( subjIter . hasNext ( ) ) { Statement st = subjIter . next ( ) ; if ( ! currentSameAs . contains ( st . getObject ( ) ) ) { Resource castedObj = ( Resource ) st . getObject ( ) ; currentSameAs . add ( castedObj ) ; findSameAsChaining ( castedObj , currentSameAs , contxts ) ; } } subjIter . close ( ) ; CloseableIteration < Statement , QueryEvaluationException > objIter = RyaDAOHelper . query ( ryaDAO , null , OWL . SAMEAS , subj , conf , contxts ) ; while ( objIter . hasNext ( ) ) { Statement st = objIter . next ( ) ; if ( ! currentSameAs . contains ( st . getSubject ( ) ) ) { Resource sameAsSubj = st . getSubject ( ) ; currentSameAs . add ( sameAsSubj ) ; findSameAsChaining ( sameAsSubj , currentSameAs , contxts ) ; } } objIter . close ( ) ; } catch ( QueryEvaluationException e ) { throw new InferenceEngineException ( e ) ; } }
todo : this chaining can be slow at query execution . the other option is to perform this in the query itself , but that will be constrained to how many levels we decide to go
