Search in sources :

Example 6 with Constant

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);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Constant(fr.lirmm.graphik.graal.api.core.Constant) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 7 with Constant

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));
}
Also used : ConversionException(fr.lirmm.graphik.util.stream.converter.ConversionException) Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Constant(fr.lirmm.graphik.graal.api.core.Constant) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 8 with Constant

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);
}
Also used : ConversionException(fr.lirmm.graphik.util.stream.converter.ConversionException) Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Constant(fr.lirmm.graphik.graal.api.core.Constant) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 9 with Constant

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));
}
Also used : ConversionException(fr.lirmm.graphik.util.stream.converter.ConversionException) Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Constant(fr.lirmm.graphik.graal.api.core.Constant) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 10 with Constant

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));
}
Also used : Constant(fr.lirmm.graphik.graal.api.core.Constant) Atom(fr.lirmm.graphik.graal.api.core.Atom) Theory(org.junit.experimental.theories.Theory)

Aggregations

Constant (fr.lirmm.graphik.graal.api.core.Constant)12 Atom (fr.lirmm.graphik.graal.api.core.Atom)11 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)9 Term (fr.lirmm.graphik.graal.api.core.Term)9 Variable (fr.lirmm.graphik.graal.api.core.Variable)7 Test (org.junit.Test)7 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)5 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)5 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)5 LinkedList (java.util.LinkedList)5 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)3 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)2 Rule (fr.lirmm.graphik.graal.api.core.Rule)2 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)2 CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)2 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)2 Store (fr.lirmm.graphik.graal.api.store.Store)1 HashSet (java.util.HashSet)1 Theory (org.junit.experimental.theories.Theory)1