Search in sources :

Example 41 with Term

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

the class Rules method computeAtomicHead.

/**
 * Generate a set of atomic head rules equivalent of the specified rule.
 *
 * @param rule
 * @return a Collection of Rule which is a decomposition of the specified rule to atomic head rules.
 */
public static Collection<Rule> computeAtomicHead(Rule rule) {
    String label = rule.getLabel();
    Collection<Rule> atomicHead = new LinkedList<Rule>();
    if (rule.getHead().isEmpty() || hasAtomicHead(rule)) {
        return Collections.<Rule>singleton(rule);
    } else {
        Predicate predicate = new Predicate("aux_" + ++auxIndex, rule.getTerms().size());
        Atom aux = DefaultAtomFactory.instance().create(predicate, rule.getTerms().toArray(new Term[rule.getTerms().size()]));
        if (label.isEmpty()) {
            atomicHead.add(DefaultRuleFactory.instance().create(rule.getBody(), new LinkedListAtomSet(aux)));
            CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
            while (it.hasNext()) {
                Atom atom = it.next();
                atomicHead.add(DefaultRuleFactory.instance().create(aux, atom));
            }
        } else {
            int i = -1;
            atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, rule.getBody(), new LinkedListAtomSet(aux)));
            CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
            while (it.hasNext()) {
                Atom atom = it.next();
                atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, aux, atom));
            }
        }
    }
    return atomicHead;
}
Also used : LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 42 with Term

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

the class DefaultInMemoryGraphStore method add.

@Override
public boolean add(Atom atom) {
    List<TermVertex> atomTerms = new LinkedList<TermVertex>();
    PredicateVertex atomPredicate;
    for (Term t : atom.getTerms()) {
        atomTerms.add(this.addTermVertex(TermVertexFactory.instance().createTerm(t)));
    }
    atomPredicate = this.addPredicateVertex(new PredicateVertex(atom.getPredicate()));
    AtomEdge atomEdge = new AtomEdge(atomPredicate, atomTerms);
    return this.addAtomEdge(atomEdge);
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList)

Example 43 with Term

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

the class TripleStoreTest method simpleTest.

@Theory
public void simpleTest(AtomSet store) throws AtomSetException, IteratorException {
    Assume.assumeTrue(store instanceof TripleStore);
    Term t1 = DefaultTermFactory.instance().createConstant("http://to.to/b");
    Term t2 = DefaultTermFactory.instance().createConstant("http://to.to/a");
    Predicate p = new Predicate("http://to.to/p", 2);
    Atom atom1 = new DefaultAtom(p, t1, t2);
    store.add(atom1);
    int i = 0;
    for (CloseableIterator<Atom> it = store.iterator(); it.hasNext(); it.next()) {
        ++i;
    }
    Assert.assertEquals(1, i);
}
Also used : TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) 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) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 44 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 45 with Term

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

the class RdbmsAtomIterator method hasNext.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean hasNext() throws IteratorException {
    if (!this.hasNextCallDone) {
        this.hasNextCallDone = true;
        while (this.predicateIt.hasNext() && (this.atomIt == null || !this.atomIt.hasNext())) {
            Predicate p = predicateIt.next();
            List<Term> terms = new LinkedList<Term>();
            VariableGenerator gen = new DefaultVariableGenerator("X");
            for (int i = 0; i < p.getArity(); ++i) {
                terms.add(gen.getFreshSymbol());
            }
            InMemoryAtomSet atomSet = new LinkedListAtomSet();
            Atom atom = new DefaultAtom(p, terms);
            atomSet.add(atom);
            ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atomSet);
            SqlHomomorphism solver = SqlHomomorphism.instance();
            try {
                this.atomIt = new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this.store));
            } catch (HomomorphismException e) {
                throw new IteratorException(e);
            }
        }
    }
    return this.atomIt != null && this.atomIt.hasNext();
}
Also used : DefaultVariableGenerator(fr.lirmm.graphik.graal.core.DefaultVariableGenerator) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) SqlHomomorphism(fr.lirmm.graphik.graal.store.rdbms.homomorphism.SqlHomomorphism) Term(fr.lirmm.graphik.graal.api.core.Term) SubstitutionIterator2AtomIterator(fr.lirmm.graphik.graal.core.stream.SubstitutionIterator2AtomIterator) LinkedList(java.util.LinkedList) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultVariableGenerator(fr.lirmm.graphik.graal.core.DefaultVariableGenerator) VariableGenerator(fr.lirmm.graphik.graal.api.core.VariableGenerator) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

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