Search in sources :

Example 66 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class NaturalRDBMSStore method add.

// /////////////////////////////////////////////////////////////////////////
// PROTECTED AND PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
protected Statement add(Statement statement, Atom atom) throws AtomSetException {
    if (!this.check(atom)) {
        // FIXME say why
        throw new UnsupportedAtomTypeException("");
    }
    try {
        DBTable table = this.createPredicateTableIfNotExist(atom.getPredicate());
        Iterator<DBColumn> cols = table.getColumns().iterator();
        Map<String, String> data = new TreeMap<String, String>();
        for (Term t : atom.getTerms()) {
            DBColumn col = cols.next();
            data.put(col.getName(), this.getConjunctiveQueryTranslator().formatFromColumnType(col, t));
        }
        String query = this.getDriver().getInsertOrIgnoreQuery(table, data);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(atom.toString() + " : " + query);
        }
        statement.addBatch(query);
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
    return statement;
}
Also used : UnsupportedAtomTypeException(fr.lirmm.graphik.graal.api.core.UnsupportedAtomTypeException) DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLException(java.sql.SQLException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap)

Example 67 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class RDF4jStore method getTerms.

@Override
public Set<Term> getTerms() throws AtomSetException {
    TreeSet<Term> set = new TreeSet<Term>();
    CloseableIterator<Term> it = this.termsIterator();
    try {
        while (it.hasNext()) {
            set.add(it.next());
        }
    } catch (IteratorException e) {
        throw new AtomSetException("An error occurs during iteration over terms", e);
    }
    return set;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) TreeSet(java.util.TreeSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 68 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class BackwardChainingTest method getBody2.

/**
 * c(X) :- b(X,X).
 *
 * getBody([X]) => b(X, X).
 *
 * @throws ParseException
 */
@Theory
public void getBody2(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X) :- q(X,X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIteratorWithoutException<? extends ConjunctiveQuery> rewIt = bc.execute(query, rules, compilation);
    boolean isFound = false;
    while (rewIt.hasNext()) {
        ConjunctiveQuery rew = rewIt.next();
        CloseableIteratorWithoutException<Atom> it = rew.getAtomSet().iterator();
        if (it.hasNext()) {
            Atom a = it.next();
            if (a.getPredicate().equals(new Predicate("q", 2))) {
                isFound = true;
                List<Term> terms = a.getTerms();
                Assert.assertEquals(terms.get(0), terms.get(1));
            }
        }
    }
    Assert.assertTrue("Rewrite not found", isFound);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 69 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class Utils method imply.

/**
 * This methods test if the first rule logically imply the second. This
 * methods works with linear rules (atomic head and body).
 *
 * @param r1
 * @param r2
 * @return true, if r1 logically imply r2.
 */
public static boolean imply(Rule r1, Rule r2) {
    Atom b1 = r1.getBody().iterator().next();
    Atom b2 = r2.getBody().iterator().next();
    Atom h1 = r1.getHead().iterator().next();
    Atom h2 = r2.getHead().iterator().next();
    if (b1.getPredicate().equals(b2.getPredicate()) && h1.getPredicate().equals(h2.getPredicate())) {
        Map<Term, Term> s = new TreeMap<Term, Term>();
        Term term;
        for (int i = 0; i < b1.getPredicate().getArity(); i++) {
            term = s.get(b1.getTerm(i));
            if (term == null)
                s.put(b1.getTerm(i), b2.getTerm(i));
            else if (!term.equals(b2.getTerm(i)))
                return false;
        }
        for (int i = 0; i < h1.getPredicate().getArity(); i++) {
            term = s.get(h1.getTerm(i));
            if (term == null)
                s.put(h1.getTerm(i), h2.getTerm(i));
            else if (!term.equals(h2.getTerm(i)))
                return false;
        }
        return true;
    }
    return false;
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 70 with Term

use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.

the class Utils method rewrite.

/**
 * Rewrite the fact q according to the unifier u.
 *
 * @param q
 *            the fact to rewrite
 * @param u
 *            the unifier between q and r
 * @return the rewrite of q according to the unifier u.
 */
public static ConjunctiveQuery rewrite(ConjunctiveQuery q, QueryUnifier u) {
    InMemoryAtomSet ajout = u.getImageOf(u.getRule().getBody());
    InMemoryAtomSet restant = u.getImageOf(AtomSetUtils.minus(q.getAtomSet(), u.getPiece()));
    ConjunctiveQuery rew = null;
    if (ajout != null && restant != null) {
        // FIXME
        InMemoryAtomSet res = AtomSetUtils.union(ajout, restant);
        List<Term> ansVar = new LinkedList<Term>();
        ansVar.addAll(q.getAnswerVariables());
        rew = DefaultConjunctiveQueryFactory.instance().create(res, ansVar);
    }
    return rew;
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) LinkedList(java.util.LinkedList)

Aggregations

Term (fr.lirmm.graphik.graal.api.core.Term)173 Atom (fr.lirmm.graphik.graal.api.core.Atom)86 LinkedList (java.util.LinkedList)41 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)36 Test (org.junit.Test)35 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)32 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)27 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)26 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)23 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)15 Rule (fr.lirmm.graphik.graal.api.core.Rule)15 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)14 TreeSet (java.util.TreeSet)12 Constant (fr.lirmm.graphik.graal.api.core.Constant)9 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9 TreeMap (java.util.TreeMap)8