use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class StarBootstrapper method exec.
// /////////////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation compilation) throws BacktrackException {
Set<Term> terms = null;
if (this.getProfiler() != null) {
this.getProfiler().start("BootstrapTime");
this.getProfiler().start("BootstrapTimeFirstPart");
}
Iterator<Atom> it;
Collection<Constant> constants = null;
Atom aa = null;
it = postAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
try {
if (constants != null && !constants.isEmpty()) {
terms = new TreeSet<Term>(TermValueComparator.instance());
for (Pair<Atom, Substitution> im : compilation.getRewritingOf(aa)) {
int pos = im.getLeft().indexOf(im.getRight().createImageOf(v.value));
CloseableIterator<Atom> match = data.match(im.getLeft());
while (match.hasNext()) {
terms.add(match.next().getTerm(pos));
}
}
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTimeFirstPart");
}
if (terms == null) {
it = postAtoms.iterator();
while (it.hasNext()) {
if (terms == null) {
terms = BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation);
} else {
terms.retainAll(BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation));
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
if (terms == null) {
terms = BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation);
} else {
terms.retainAll(BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation));
}
}
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTime");
}
if (terms == null) {
return data.termsIterator();
} else {
return new CloseableIteratorAdapter<Term>(terms.iterator());
}
} catch (AtomSetException e) {
throw new BacktrackException(e);
} catch (IteratorException e) {
throw new BacktrackException(e);
}
}
use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class Atom2SubstitutionConverterTest method basic2.
@Test
public void basic2() throws ParseException {
// given
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);
// when
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();
}
// then
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));
}
use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class Atom2SubstitutionConverterTest method wrongUsage.
@Test
public void wrongUsage() throws ParseException {
// given
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);
// when
Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList);
Substitution s = null;
try {
s = converter.convert(DlgpParser.parseAtom("p(a, b)."));
} catch (ConversionException e) {
fail();
}
// then
Constant a = DefaultTermFactory.instance().createConstant("a");
System.out.println(s);
}
use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class Atom2SubstitutionConverterTest method githubIssue2variantWithConstant.
@Test
public void githubIssue2variantWithConstant() throws ParseException {
// given
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);
// when
Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList);
Substitution s = null;
try {
s = converter.convert(DlgpParser.parseAtom("p(a)."));
} catch (ConversionException e) {
fail();
}
// then
Constant a = DefaultTermFactory.instance().createConstant("a");
assertEquals(a, s.createImageOf(x));
assertEquals(b, s.createImageOf(b));
}
use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class AtomTest method testGetConstants.
/**
* Test method for
* {@link fr.lirmm.graphik.graal.core.DefaultAtom#getConstants()}.
*/
@Theory
public void testGetConstants(AtomFactory factory) {
// given
Atom a = factory.create(TestUtils.pXA);
// when
Set<Constant> constants = a.getConstants();
// then
Assert.assertEquals(1, constants.size());
Assert.assertTrue(constants.contains(TestUtils.A));
}
Aggregations