import java.net.URI;
import java.util.List;
import java.util.Vector;

import org.junit.Test;
import org.obda.query.domain.Atom;
import org.obda.query.domain.CQIE;
import org.obda.query.domain.DatalogProgram;
import org.obda.query.domain.Predicate;
import org.obda.query.domain.PredicateFactory;
import org.obda.query.domain.Term;
import org.obda.query.domain.TermFactory;
import org.obda.query.domain.imp.AtomImpl;
import org.obda.query.domain.imp.BasicPredicateFactoryImpl;
import org.obda.query.domain.imp.CQIEImpl;
import org.obda.query.domain.imp.DatalogProgramImpl;
import org.obda.query.domain.imp.TermFactoryImpl;
import org.obda.query.domain.imp.UndistinguishedVariable;
import org.obda.reformulation.dllite.DLRPerfectReformulator;
import org.obda.reformulation.dllite.QueryRewriter;
import org.obda.reformulation.domain.Assertion;

import junit.framework.TestCase;

public class UnificationTest2 extends TestCase {

	/**
	 * Test method for
	 * {@link org.obda.reformulation.dllite.DLRPerfectReformulator#rewrite(org.obda.query.domain.Query)}
	 * .
	 * 
	 * Check if MGU generation/application works properly with multiple atoms sharing variables
	 * 
	 * q(x,y) :- R(x,#) R(#,y), S(x,#)
	 * 
	 * @throws Exception
	 */
	@Test
	public void test_1() throws Exception {

		TermFactory factory = TermFactoryImpl.getInstance();
		PredicateFactory predFac = BasicPredicateFactoryImpl.getInstance();

		Term t1 = factory.createVariable("x");
		Term t2 = factory.createVariable("y");
		Term t3 = factory.createVariable("x");

		Predicate r1 = predFac.getPredicate(URI.create("R"), 2);
		Predicate r2 = predFac.getPredicate(URI.create("R"), 2);
		Predicate s = predFac.getPredicate(URI.create("S"), 2);
		Predicate p = predFac.getPredicate(URI.create("p"), 2);

		List<Term> terms1 = new Vector<Term>();
		terms1.add(t1);
		terms1.add(new UndistinguishedVariable());
		List<Term> terms2 = new Vector<Term>();
		terms2.add(new UndistinguishedVariable());
		terms2.add(t2);
		List<Term> terms3 = new Vector<Term>();
		terms3.add(t3);
		terms3.add(new UndistinguishedVariable());
		List<Term> terms4 = new Vector<Term>();
		terms4.add(t3.copy());
		terms4.add(t2.copy());

		Atom a1 = new AtomImpl(r1, terms1);
		Atom a2 = new AtomImpl(r2, terms2);
		Atom a3 = new AtomImpl(s, terms3);
		Atom head = new AtomImpl(p, terms4);

		Vector<Atom> body = new Vector<Atom>();
		body.add(a1);
		body.add(a2);
		body.add(a3);
		CQIE query = new CQIEImpl(head, body, true);
		DatalogProgram prog = new DatalogProgramImpl();
		prog.appendRule(query);

		List<Assertion> list = new Vector<Assertion>();
		QueryRewriter rew = new DLRPerfectReformulator(list);
		DatalogProgram aux = (DatalogProgram) rew.rewrite(prog);

		assertEquals(2, aux.getRules().size());
		// note: aux.getRules().get(0) should be the original one
		CQIE cq = aux.getRules().get(1);
		List<Atom> newbody = cq.getBody();

		assertEquals(2, newbody.size());
		Atom at1 = newbody.get(0);
		Atom at2 = newbody.get(1);

		Term term1 = at1.getTerms().get(0);
		Term term2 = at1.getTerms().get(1);
		Term term3 = at2.getTerms().get(0);
		Term term4 = at2.getTerms().get(1);

		assertEquals("x", term1.getName());
		assertEquals("y", term2.getName());
		assertEquals("x", term3.getName());
		assertEquals("#", term4.getName());

	}

}

@@ -63,24 +63,60 @@
 .Vector;%0D%0A%0D%0A
+import junit.framework.TestCase;%0D%0A%0D%0A
 import org.j
@@ -374,51 +374,8 @@
 m;%0D%0A
-import org.obda.query.domain.TermFactory;%0D%0A
 impo
@@ -628,67 +628,8 @@
 l;%0D%0A
-import org.obda.query.domain.imp.UndistinguishedVariable;%0D%0A
 impo
@@ -794,44 +794,8 @@
 %0D%0A%0D%0A
-import junit.framework.TestCase;%0D%0A%0D%0A
 publ
@@ -978,25 +978,24 @@
 %0D%0A%09 * .%0D%0A%09 *
- 
 %0D%0A%09 * Check 
@@ -1077,25 +1077,24 @@
 riables%0D%0A%09 *
- 
 %0D%0A%09 * q(x,y)
@@ -1123,17 +1123,16 @@
 ,#)%0D%0A%09 *
- 
 %0D%0A%09 * @t
@@ -1219,16 +1219,20 @@
 mFactory
+Impl
  factory
@@ -1484,35 +1484,38 @@
 te r1 = predFac.
-get
+create
 Predicate(URI.cr
@@ -1547,35 +1547,38 @@
 te r2 = predFac.
-get
+create
 Predicate(URI.cr
@@ -1609,35 +1609,38 @@
 ate s = predFac.
-get
+create
 Predicate(URI.cr
@@ -1679,19 +1679,22 @@
 predFac.
-get
+create
 Predicat
@@ -1790,28 +1790,38 @@
 %09terms1.add(
-new 
+factory.create
 Undistinguis
@@ -1889,28 +1889,38 @@
 %09terms2.add(
-new 
+factory.create
 Undistinguis
@@ -2030,20 +2030,30 @@
 ms3.add(
-new 
+factory.create
 Undistin

Rename the method name: getPredicate() into createPredicate(). Also, the creation of undistinguished variable is through the term factory.