use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class Atom2SubstitutionConverterTest method githubIssue2variant1.
@Test
public void githubIssue2variant1() throws ParseException {
// given
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);
// 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(y, s.createImageOf(y));
}
use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class Atom2SubstitutionConverterTest method basic.
@Test
public void basic() throws ParseException {
// given
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);
// 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));
}
use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class StatBootstrapper method exec.
// /////////////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation rc) throws BacktrackException {
if (!(data instanceof Store)) {
return fallback.exec(v, preAtoms, postAtoms, data, rc);
}
Store store = (Store) data;
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 HashSet<Term>();
for (Pair<Atom, Substitution> im : rc.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) {
Atom a = null, tmp;
double probaA = 1.1;
it = postAtoms.iterator();
while (it.hasNext()) {
tmp = it.next();
double p = ProbaUtils.computeProba(tmp, store, rc);
if (p < probaA) {
a = tmp;
p = probaA;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
tmp = it.next();
double p = ProbaUtils.computeProba(tmp, store, rc);
if (p < probaA) {
a = tmp;
p = probaA;
}
}
terms = BootstrapperUtils.computeCandidatesOverRewritings(a, v, data, rc);
}
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 FrontierRestrictedChaseHaltingConditionTest method test.
@Test
public void test() throws IteratorException, HomomorphismFactoryException, HomomorphismException {
InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create(DlgpParser.parseAtom("p(a,b)."));
Rule rule = DlgpParser.parseRule("p(X,Z):-p(X,Y).");
Variable x = DefaultTermFactory.instance().createVariable("X");
Variable y = DefaultTermFactory.instance().createVariable("Y");
Constant a = DefaultTermFactory.instance().createConstant("a");
Constant b = DefaultTermFactory.instance().createConstant("b");
Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
s.put(x, a);
s.put(y, b);
FrontierRestrictedChaseHaltingCondition condition = new FrontierRestrictedChaseHaltingCondition();
CloseableIterator<Atom> toAdd = condition.apply(rule, s, atomset);
Assert.assertTrue(toAdd.hasNext());
Atom atom1 = toAdd.next();
atomset.add(atom1);
Assert.assertFalse(toAdd.hasNext());
toAdd.close();
s = DefaultSubstitutionFactory.instance().createSubstitution();
s.put(x, a);
s.put(y, atom1.getTerm(1));
toAdd = condition.apply(rule, s, atomset);
Assert.assertFalse(toAdd.hasNext());
toAdd.close();
}
use of fr.lirmm.graphik.graal.api.core.Constant in project graal by graphik-team.
the class ConjunctiveQueryWithFixedVariables method appendTo.
@Override
public void appendTo(StringBuilder sb) {
for (Constant t : this.atomSet.getConstants()) sb.append(t).append(',');
sb.append("), ANS(");
for (Term t : this.answerVariables) sb.append(t).append(',');
sb.append(") :- ");
sb.append(this.atomSet);
}
Aggregations