Atom2SubstitutionConverter implements Converter<Atom, Substitution> { @Override public Substitution convert(Atom object) throws ConversionException { Substitution s = new HashMapSubstitution(); for (Term var : ans) { if(var.isVariable() && variables.containsKey(var)) { s.put((Variable) var, object.getTerm(variables.get(var))); } } return s; } Atom2SubstitutionConverter(Atom query, List<Term> ans);  Atom2SubstitutionConverter(Atom query, List<Term> ans, Substitution rew); @Override Substitution convert(Atom object);  }
@Test public void basic() throws ParseException { Predicate p = DefaultPredicateFactory.instance().create("p", 1); Variable x = DefaultTermFactory.instance().createVariable("X"); Atom queryAtom = new DefaultAtom(p, x); List<Term> ansList = new LinkedList<>(); ansList.add(x); Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList); Substitution s = null; try { s = converter.convert(DlgpParser.parseAtom("p(a).")); } catch (ConversionException e) { fail(); } Constant a = DefaultTermFactory.instance().createConstant("a"); assertEquals(a, s.createImageOf(x)); }
@Test public void basic2() throws ParseException { Predicate p = DefaultPredicateFactory.instance().create("p", 3); Variable x = DefaultTermFactory.instance().createVariable("X"); Variable y = DefaultTermFactory.instance().createVariable("Y"); Variable z = DefaultTermFactory.instance().createVariable("Z"); Atom queryAtom = new DefaultAtom(p, x, y, z); List<Term> ansList = new LinkedList<>(); ansList.add(x); ansList.add(z); Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList); Substitution s = null; try { s = converter.convert(DlgpParser.parseAtom("p(a,b,c).")); } catch (ConversionException e) { fail(); } Constant a = DefaultTermFactory.instance().createConstant("a"); Constant c = DefaultTermFactory.instance().createConstant("c"); assertEquals(a, s.createImageOf(x)); assertEquals(y, s.createImageOf(y)); assertEquals(c, s.createImageOf(z)); }
@Test public void githubIssue2() throws ParseException { Predicate p = DefaultPredicateFactory.instance().create("p", 1); Variable x = DefaultTermFactory.instance().createVariable("X"); Variable y = DefaultTermFactory.instance().createVariable("Y"); Atom queryAtom = new DefaultAtom(p, x); List<Term> ansList = new LinkedList<>(); ansList.add(y); Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList); Substitution s = null; try { s = converter.convert(DlgpParser.parseAtom("p(a).")); } catch (ConversionException e) { fail(); } assertEquals(x, s.createImageOf(x)); assertEquals(y, s.createImageOf(y)); }
@Test public void githubIssue2variant1() throws ParseException { Predicate p = DefaultPredicateFactory.instance().create("p", 1); Variable x = DefaultTermFactory.instance().createVariable("X"); Variable y = DefaultTermFactory.instance().createVariable("Y"); Atom queryAtom = new DefaultAtom(p, x); List<Term> ansList = new LinkedList<>(); ansList.add(x); ansList.add(y); Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList); Substitution s = null; try { s = converter.convert(DlgpParser.parseAtom("p(a).")); } catch (ConversionException e) { fail(); } Constant a = DefaultTermFactory.instance().createConstant("a"); assertEquals(a, s.createImageOf(x)); assertEquals(y, s.createImageOf(y)); }
@Test public void githubIssue2variantWithConstant() throws ParseException { Predicate p = DefaultPredicateFactory.instance().create("p", 1); Variable x = DefaultTermFactory.instance().createVariable("X"); Constant b = DefaultTermFactory.instance().createConstant("b"); Atom queryAtom = new DefaultAtom(p, x); List<Term> ansList = new LinkedList<>(); ansList.add(x); ansList.add(b); Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList); Substitution s = null; try { s = converter.convert(DlgpParser.parseAtom("p(a).")); } catch (ConversionException e) { fail(); } Constant a = DefaultTermFactory.instance().createConstant("a"); assertEquals(a, s.createImageOf(x)); assertEquals(b, s.createImageOf(b)); }
@Test public void wrongUsage() throws ParseException { Predicate p = DefaultPredicateFactory.instance().create("p", 1); Variable x = DefaultTermFactory.instance().createVariable("X"); Atom queryAtom = new DefaultAtom(p, x, x); List<Term> ansList = new LinkedList<>(); ansList.add(x); Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList); Substitution s = null; try { s = converter.convert(DlgpParser.parseAtom("p(a, b).")); } catch (ConversionException e) { fail(); } System.out.println(s); }