use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.
the class OWL2ParserTest method assertionDataProperty.
@Test
public void assertionDataProperty() throws OWL2ParserException, IteratorException {
OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:DatatypeProperty . " + ":i1 :p 7 ." + "");
boolean found = false;
while (parser.hasNext()) {
Object o = parser.next();
if (!(o instanceof Prefix)) {
AtomSet atomset = (AtomSet) o;
Atom a = atomset.iterator().next();
Assert.assertEquals(P, a.getPredicate());
Iterator<Term> it = a.iterator();
Assert.assertEquals(I1, it.next());
Assert.assertEquals(L1, it.next());
found = true;
}
}
parser.close();
Assert.assertTrue("Number of assertions found:", found);
}
use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.
the class OWL2ParserTest method assertionWithExistential.
@Test
public void assertionWithExistential() throws OWL2ParserException {
try {
OWL2Parser parser = new OWL2Parser(PREFIXES + ":A rdf:type owl:Class . " + ":p rdf:type owl:ObjectProperty . " + ":i1 :p [a :A] ." + "");
int nbFacts = 0;
int nbAtoms = 0;
while (parser.hasNext()) {
Object o = parser.next();
if (o instanceof InMemoryAtomSet) {
++nbFacts;
CloseableIterator<Atom> it = ((AtomSet) o).iterator();
while (it.hasNext()) {
it.next();
++nbAtoms;
}
}
}
parser.close();
Assert.assertEquals("Number of facts found:", 1, nbFacts);
Assert.assertEquals("Number of atoms found:", 2, nbAtoms);
} catch (Throwable e) {
Assert.fail("An exception was found: " + e);
}
}
use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.
the class AdHocConjunctiveQueryTranslator method translate.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public SQLQuery translate(ConjunctiveQuery cquery) throws AtomSetException {
if (cquery.getAtomSet().isEmpty()) {
return SQLQuery.emptyInstance();
}
AtomSet atomSet = cquery.getAtomSet();
StringBuilder fields = new StringBuilder();
StringBuilder tables = new StringBuilder();
StringBuilder where = new StringBuilder();
HashMap<Atom, String> tableNames = new HashMap<Atom, String>();
HashMap<String, String> lastOccurrence = new HashMap<String, String>();
ArrayList<String> constants = new ArrayList<String>();
ArrayList<String> equivalences = new ArrayList<String>();
TreeMap<Term, String> columns = new TreeMap<Term, String>();
int count = -1;
CloseableIterator<Atom> it = atomSet.iterator();
try {
while (it.hasNext()) {
Atom atom = it.next();
String tableName = "atom" + ++count;
tableNames.put(atom, tableName);
}
// Create WHERE clause
it = atomSet.iterator();
while (it.hasNext()) {
Atom atom = it.next();
String currentAtom = tableNames.get(atom) + ".";
int position = 0;
for (Term term : atom.getTerms()) {
String thisTerm = currentAtom + AbstractRdbmsConjunctiveQueryTranslator.PREFIX_TERM_FIELD + position;
if (term.isConstant()) {
constants.add(thisTerm + " = '" + term.getIdentifier().toString() + "'");
} else {
if (lastOccurrence.containsKey(term.getIdentifier().toString())) {
equivalences.add(lastOccurrence.get(term.getIdentifier().toString()) + " = " + thisTerm);
}
lastOccurrence.put(term.getIdentifier().toString(), thisTerm);
if (cquery.getAnswerVariables().contains(term))
columns.put(term, thisTerm + " as " + term.getIdentifier().toString());
}
++position;
}
}
} catch (IteratorException e) {
throw new AtomSetException(e);
}
for (String equivalence : equivalences) {
if (where.length() != 0)
where.append(" AND ");
where.append(equivalence);
}
for (String constant : constants) {
if (where.length() != 0)
where.append(" AND ");
where.append(constant);
}
// Create FROM clause
DBTable table = null;
for (Map.Entry<Atom, String> entries : tableNames.entrySet()) {
if (tables.length() != 0)
tables.append(", ");
table = store.getPredicateTableIfExist(entries.getKey().getPredicate());
if (table == null)
return SQLQuery.hasSchemaErrorInstance();
else
tables.append(table.getName());
tables.append(" as ");
tables.append(entries.getValue());
}
// Create SELECT clause
for (Term t : cquery.getAnswerVariables()) {
if (fields.length() != 0)
fields.append(", ");
if (t.isConstant()) {
fields.append("'");
fields.append(t.getIdentifier());
fields.append("'");
} else {
fields.append(columns.get(t));
}
}
StringBuilder query = new StringBuilder("SELECT DISTINCT ");
if (fields.length() > 0)
query.append(fields);
else
query.append("1");
query.append(" FROM ");
if (tables.length() > 0)
query.append(tables);
else
query.append(AdHocRdbmsStore.TEST_TABLE_NAME);
if (where.length() > 0)
query.append(" WHERE ").append(where);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Generated SQL query :" + cquery + " --> " + query.toString());
return new SQLQuery(query.toString());
}
use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method test2.
@Theory
public void test2(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
store.add(DlgpParser.parseAtom("<P>(a,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<Q>(X,Y) :- <P>(Y,X)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
CloseableIterator<Substitution> results = h.execute(DlgpParser.parseQuery("?(X,Y) :- <Q>(X,Y)."), store, comp);
Assert.assertTrue(results.hasNext());
Substitution next = results.next();
Assert.assertEquals(DefaultTermFactory.instance().createConstant("a"), next.createImageOf(DefaultTermFactory.instance().createVariable("Y")));
Assert.assertEquals(DefaultTermFactory.instance().createConstant("b"), next.createImageOf(DefaultTermFactory.instance().createVariable("X")));
Assert.assertFalse(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method issue34.
@Theory
public void issue34(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
store.add(DlgpParser.parseAtom("<Q>(a,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<P>(X,Y) :- <Q>(Y,X)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
InMemoryAtomSet query1 = new LinkedListAtomSet();
query1.add(DlgpParser.parseAtom("<P>(a,Y)."));
CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query1), store, comp);
Assert.assertFalse(results.hasNext());
results.close();
}
Aggregations