Exemplo n.º 1
0
  /**
   * Execute transaction.
   *
   * @param config the Config object
   * @param transactionLevel the transaction level
   * @param atom the atom operation
   * @return true if transaction executing succeed otherwise false
   */
  boolean tx(Config config, int transactionLevel, IAtom atom) {
    Connection conn = config.getThreadLocalConnection();
    if (conn != null) { // Nested transaction support
      try {
        if (conn.getTransactionIsolation() < transactionLevel)
          conn.setTransactionIsolation(transactionLevel);
        boolean result = atom.run();
        if (result) return true;
        throw new NestedTransactionHelpException(
            "Notice the outer transaction that the nested transaction return false"); // important:can not return false
      } catch (SQLException e) {
        throw new ActiveRecordException(e);
      }
    }

    Boolean autoCommit = null;
    try {
      conn = config.getConnection();
      autoCommit = conn.getAutoCommit();
      config.setThreadLocalConnection(conn);
      conn.setTransactionIsolation(transactionLevel);
      conn.setAutoCommit(false);
      boolean result = atom.run();
      if (result) conn.commit();
      else conn.rollback();
      return result;
    } catch (NestedTransactionHelpException e) {
      if (conn != null)
        try {
          conn.rollback();
        } catch (Exception e1) {
          LogKit.error(e1.getMessage(), e1);
        }
      LogKit.logNothing(e);
      return false;
    } catch (Throwable t) {
      if (conn != null)
        try {
          conn.rollback();
        } catch (Exception e1) {
          LogKit.error(e1.getMessage(), e1);
        }
      throw t instanceof RuntimeException ? (RuntimeException) t : new ActiveRecordException(t);
    } finally {
      try {
        if (conn != null) {
          if (autoCommit != null) conn.setAutoCommit(autoCommit);
          conn.close();
        }
      } catch (Throwable t) {
        LogKit.error(
            t.getMessage(),
            t); // can not throw exception here, otherwise the more important exception in previous
                // catch block can not be thrown
      } finally {
        config.removeThreadLocalConnection(); // prevent memory leak
      }
    }
  }
Exemplo n.º 2
0
 /** Method to test whether the class complies with RFC #9. */
 @Test
 @Override
 public void testToString() {
   IAtom atom = (IPseudoAtom) newChemObject();
   String description = atom.toString();
   for (int i = 0; i < description.length(); i++) {
     Assert.assertTrue(description.charAt(i) != '\n');
     Assert.assertTrue(description.charAt(i) != '\r');
   }
 }
 private void processAtomsBlock(int lineCount, IAtomContainer container) throws IOException {
   for (int i = 0; i < lineCount; i++) {
     String line = input.readLine();
     int atomicNumber = Integer.parseInt(line.substring(7, 10).trim());
     IAtom atom = container.getBuilder().newAtom();
     atom.setAtomicNumber(atomicNumber);
     atom.setSymbol(Symbols.byAtomicNumber[atomicNumber]);
     container.addAtom(atom);
   }
 }
Exemplo n.º 4
0
  @Test
  public void testPseudoAtom_IAtom() {
    IChemObject object = newChemObject();
    IAtom atom = object.getBuilder().newInstance(IAtom.class, "C");
    Point3d fract = new Point3d(0.5, 0.5, 0.5);
    Point3d threeD = new Point3d(0.5, 0.5, 0.5);
    Point2d twoD = new Point2d(0.5, 0.5);
    atom.setFractionalPoint3d(fract);
    atom.setPoint3d(threeD);
    atom.setPoint2d(twoD);

    IPseudoAtom a = object.getBuilder().newInstance(IPseudoAtom.class, atom);
    assertEquals(fract, a.getFractionalPoint3d(), 0.0001);
    assertEquals(threeD, a.getPoint3d(), 0.0001);
    assertEquals(twoD, a.getPoint2d(), 0.0001);
  }
Exemplo n.º 5
0
 /** Overwrite the method in {@link AbstractAtomTest} to always expect zero hydrogen counts. */
 @Test
 public void testGetHydrogenCount() {
   // expect zero by definition
   IAtom a = (IAtom) newChemObject();
   Assert.assertNull(a.getImplicitHydrogenCount());
   a.setImplicitHydrogenCount(5);
   Assert.assertEquals(5, a.getImplicitHydrogenCount().intValue());
   a.setImplicitHydrogenCount(null);
   Assert.assertNull(a.getImplicitHydrogenCount());
 }
Exemplo n.º 6
0
  /** Overwrite the method in {@link AbstractAtomTypeTest} to always expect zero stereo parity. */
  @Test
  @Override
  public void testClone_StereoParity() throws Exception {
    IAtom atom = (IAtom) newChemObject();
    atom.setStereoParity(3);
    IAtom clone = (IAtom) atom.clone();

    // test cloning
    atom.setStereoParity(4);
    Assert.assertEquals(0, clone.getStereoParity().intValue());
  }
Exemplo n.º 7
0
  /** Overwrite the method in {@link AbstractAtomTest} to always expect zero hydrogen counts. */
  @Test
  @Override
  public void testClone_HydrogenCount() throws Exception {
    IAtom atom = (IAtom) newChemObject();
    atom.setImplicitHydrogenCount(3);
    IAtom clone = (IAtom) atom.clone();

    // test cloning
    atom.setImplicitHydrogenCount(4);
    Assert.assertEquals(3, clone.getImplicitHydrogenCount().intValue());
  }
  /**
   * Search if the setOfAtomContainer contains the atomContainer
   *
   * @param set ISetOfAtomContainer object where to search
   * @param atomContainer IAtomContainer to search
   * @return True, if the atomContainer is contained
   */
  private boolean existAC(IAtomContainerSet set, IAtomContainer atomContainer) {

    IAtomContainer acClone = null;
    try {
      acClone = (IMolecule) atomContainer.clone();
      if (!lookingSymmetry) {
        /*remove all aromatic flags*/
        for (IAtom atom : acClone.atoms()) atom.setFlag(CDKConstants.ISAROMATIC, false);
        for (IBond bond : acClone.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
      }
    } catch (CloneNotSupportedException e1) {
      e1.printStackTrace();
    }

    for (int i = 0; i < acClone.getAtomCount(); i++)
      //			if(acClone.getAtom(i).getID() == null)
      acClone.getAtom(i).setID("" + acClone.getAtomNumber(acClone.getAtom(i)));

    if (lookingSymmetry) {
      try {
        CDKHueckelAromaticityDetector.detectAromaticity(acClone);
      } catch (CDKException e) {
        e.printStackTrace();
      }
    } else {
      if (!lookingSymmetry) {
        /*remove all aromatic flags*/
        for (IAtom atom : acClone.atoms()) atom.setFlag(CDKConstants.ISAROMATIC, false);
        for (IBond bond : acClone.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
      }
    }
    for (int i = 0; i < set.getAtomContainerCount(); i++) {
      IAtomContainer ss = set.getAtomContainer(i);
      for (int j = 0; j < ss.getAtomCount(); j++)
        //				if(ss.getAtom(j).getID() == null)
        ss.getAtom(j).setID("" + ss.getAtomNumber(ss.getAtom(j)));

      try {

        if (!lookingSymmetry) {
          QueryAtomContainer qAC =
              QueryAtomContainerCreator.createSymbolChargeIDQueryContainer(acClone);
          if (UniversalIsomorphismTester.isIsomorph(ss, qAC)) {
            QueryAtomContainer qAC2 =
                QueryAtomContainerCreator.createSymbolAndBondOrderQueryContainer(acClone);
            if (UniversalIsomorphismTester.isIsomorph(ss, qAC2)) return true;
          }
        } else {
          QueryAtomContainer qAC =
              QueryAtomContainerCreator.createSymbolAndChargeQueryContainer(acClone);
          CDKHueckelAromaticityDetector.detectAromaticity(ss);
          if (UniversalIsomorphismTester.isIsomorph(ss, qAC)) return true;
        }

      } catch (CDKException e1) {
        System.err.println(e1);
        logger.error(e1.getMessage());
        logger.debug(e1);
      }
    }
    return false;
  }