package ws.prova.test;

/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.mandarax.kernel.ResultSet;
import ws.prova.Communicator;

import java.util.List;

/**
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 2.0 <25 March 2007>
 *          Test synchronous queries.
 *          Now updated to using Communicator instead of reagent.
 *          <p>Title: Prova </p>
 *          <p>Description: Test synchronous queries</p>
 *          <p>Copyright: Copyright (C) 2002-2007</p>
 *          <p>Company: City University, London, United Kingdom</p>
 */

public class reagentTest extends TestCase {
	static final String kAgent = "prova";
	static final String kPort = null;
	static final String rulebase = "prova-examples/test001.prova";

	static Communicator comm = new Communicator(kAgent, kPort, rulebase, 0, Communicator.SYNC);

	public reagentTest(String s) {
		super(s);
	}

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public static Test suite() {
		TestSuite suite = new TestSuite(reagentTest.class);
		return suite;
	}

	/**
	 * Test ws.prova.reagent.consultSync by running an example of Alban Peignier
	 */
	public void testSimpleSolve() throws Exception {
		String input = ":- solve(parent(Parent,\"anticoagulant\")).";

		List resultSets = comm.consultSync(input, "testSimpleSolve", new Object[0]);
		// Make sure there is only one resultset since we had only one goal
		assertTrue(resultSets.size() == 1);
		// Access the first resultset
		ResultSet resultSet = (ResultSet) resultSets.get(0);
		// Position the result index at the first result (internally, pos = 0)
		// If we do not do this, the resultset will be positioned at the last result
		resultSet.first();
		// Now pos is 0
		assertEquals(0, resultSet.getCursorPosition());
		Object value = resultSet.getResult(Object.class, "Parent");
		assertEquals("molecular_function", value);
	}
}