public void testCycFormula() {
    System.out.println("\n*** testCycFormula ***");
    final CycFormulaSentence isaXThing = CycFormulaSentence.makeCycFormulaSentence(ISA, VAR_X, THING);
    assertEquals(isaXThing.getFirstArgPositionForTerm(isaXThing), ArgPosition.TOP);
    final Set<CycConstant> gatheredConstants = isaXThing.treeGather(CycConstant.class);
    assertEquals(2, gatheredConstants.size());
    assertTrue(isaXThing.treeContains(isaXThing));
    assertTrue(isaXThing.treeContains(ISA));
    assertTrue(isaXThing.treeContains(VAR_X));
    assertTrue(isaXThing.treeContains(THING));
    assertTrue(isaXThing.contains(ISA));
    assertTrue(isaXThing.contains(VAR_X));
    assertTrue(isaXThing.contains(THING));
    final CycFormulaSentence someXIsaThing = CycFormulaSentence.makeCycFormulaSentence(
            CycFormulaSentence.THERE_EXISTS, VAR_X, isaXThing);
    assertTrue(someXIsaThing.contains(isaXThing));
    assertTrue(someXIsaThing.treeContains(isaXThing));
    assertTrue(someXIsaThing.treeContains(ISA));
    assertTrue(someXIsaThing.treeContains(VAR_X));
    assertTrue(someXIsaThing.treeContains(THING));
    assertEquals(new ArgPosition(1), isaXThing.getFirstArgPositionForTerm(VAR_X));
    assertEquals(new ArgPosition(2, 2), someXIsaThing.getFirstArgPositionForTerm(THING));
    {
      assertEquals(new ArrayList(Arrays.asList(VAR_X)), new ArrayList(isaXThing.findFreeVariables()));
      assertTrue(someXIsaThing.findFreeVariables().isEmpty());
      final CycFormulaSentence everyXIsaThing = CycFormulaSentence.makeCycFormulaSentence(
              CycFormulaSentence.FOR_ALL, VAR_X, isaXThing);
      assertTrue(everyXIsaThing.findFreeVariables().isEmpty());
      final CycFormulaSentence isaXY = CycFormulaSentence.makeCycFormulaSentence(ISA, VAR_X, VAR_Y);
      assertEquals(new ArrayList(Arrays.asList(VAR_X, VAR_Y)), new ArrayList(isaXY.findFreeVariables()));
      final CycFormulaSentence conj = CycFormulaSentence.makeConjunction(isaXThing, isaXY);
      assertEquals(new ArrayList(Arrays.asList(VAR_X, VAR_Y)), new ArrayList(conj.findFreeVariables()));
    }
    { //getSpecifiedObject()
      CycFormula formula2 = new CycFormula(getCyc().makeCycList("(1 (2 3 (4)) 5)"));
      ArgPosition pathSpecification = new ArgPosition(0);
      Object obj = formula2.getSpecifiedObject(pathSpecification);
      Object expectedObj = Integer.valueOf(1);
      assertEquals(expectedObj, obj);

      pathSpecification = new ArgPosition(1);
      obj = formula2.getSpecifiedObject(pathSpecification);
      expectedObj = formula2.getArg1();
      assertEquals(expectedObj, obj);

      pathSpecification = new ArgPosition(2);
      obj = formula2.getSpecifiedObject(pathSpecification);
      expectedObj = formula2.getArg2();
      assertEquals(expectedObj, obj);

      pathSpecification = new ArgPosition(1, 2, 0);
      obj = formula2.getSpecifiedObject(pathSpecification);
      expectedObj = 4;
      assertEquals(expectedObj, obj);

      // setSpecifedObject
      pathSpecification = new ArgPosition(0);
      formula2.setSpecifiedObject(pathSpecification, "a");
      expectedObj = new CycFormula(getCyc().makeCycList("(\"a\" (2 3 (4)) 5)"));
      assertEquals(expectedObj, formula2);

      pathSpecification = new ArgPosition(2);
      formula2.setSpecifiedObject(pathSpecification, "b");
      expectedObj = new CycFormula(getCyc().makeCycList("(\"a\" (2 3 (4)) \"b\")"));
      assertEquals(expectedObj, formula2);

      pathSpecification = new ArgPosition(1, 2, 0);
      formula2.setSpecifiedObject(pathSpecification, "c");
      expectedObj = new CycFormula(getCyc().makeCycList("(\"a\" (2 3 (\"c\")) \"b\")"));
      assertEquals(expectedObj, formula2);

      try {
        CycNart cycNart = new CycNart(ARITY_RELATION_FN, 1);
        formula2.addArg(cycNart);
        expectedObj = new CycFormula(getCyc().canonicalizeList(getCyc().makeCycList("(\"a\" (2 3 (\"c\")) \"b\" (#$ArityRelationFn 2))")));
        pathSpecification = new ArgPosition(3, 1);
        formula2.setSpecifiedObject(pathSpecification, 2);
        assertEquals(expectedObj, formula2);
      } catch (UnknownHostException ex) {
        Logger.getLogger(UnitTest.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
        Logger.getLogger(UnitTest.class.getName()).log(Level.SEVERE, null, ex);
      } catch (CycApiException ex) {
        Logger.getLogger(UnitTest.class.getName()).log(Level.SEVERE, null, ex);
      }

      // test getArgPositionsForTerm

      CycFormula list = new CycFormula(
              makeCycList(makeCycList("c", "1", "2"), "a", "b", "c",
              makeCycList("a", makeCycList("c", "10", "11"), "c", "2")));
      assertEquals("((\"c\" \"1\" \"2\") \"a\" \"b\" \"c\" (\"a\" (\"c\" \"10\" \"11\") \"c\" \"2\"))", list.toString());
      Set<ArgPosition> result = list.getArgPositionsForTerm("a");
      assertEquals(new HashSet(Arrays.asList(new ArgPosition(1), new ArgPosition(4, 0))), result);
      Set<ArgPosition> result1 = list.getArgPositionsForTerm("c");
      assertEquals(new HashSet(Arrays.asList(new ArgPosition(0, 0), new ArgPosition(3),
              new ArgPosition(4, 1, 0), new ArgPosition(4, 2))), result1);
      ArgPosition result2 = list.getFirstArgPositionForTerm("d");
      assertEquals(null, result2);
    }
    System.out.println("*** testCycFormula OK ***");
  }