Search in sources :

Example 21 with Term

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

the class DlgpParserTest method backslashedDoubleQuoteTest.

// /////////////////////////////////////////////////////////////////////////
// MISCS
// /////////////////////////////////////////////////////////////////////////
@Test
public void backslashedDoubleQuoteTest() throws ParseException {
    Atom a = DlgpParser.parseAtom("p(\"test\\\"test\").");
    Term identifier = a.getTerm(0);
    Assert.assertTrue(identifier instanceof Literal);
    URI datatype = ((Literal) identifier).getDatatype();
    Assert.assertEquals(URIUtils.XSD_STRING, datatype);
    Object value = ((Literal) identifier).getValue();
    Assert.assertEquals("test\"test", value);
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultURI(fr.lirmm.graphik.util.DefaultURI) URI(fr.lirmm.graphik.util.URI) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 22 with Term

use of fr.lirmm.graphik.graal.api.core.Term 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));
}
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 23 with Term

use of fr.lirmm.graphik.graal.api.core.Term 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));
}
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 24 with Term

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

the class DlgpWriter method writeStandardAtom.

@Override
protected void writeStandardAtom(Atom atom) throws IOException {
    this.writePredicate(atom.getPredicate());
    this.write('(');
    boolean isFirst = true;
    for (Term t : atom.getTerms()) {
        if (isFirst) {
            isFirst = false;
        } else {
            this.write(", ");
        }
        this.writeTerm(t);
    }
    this.write(')');
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term)

Example 25 with Term

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

the class DlgpWriterTest method writeConstant.

@Test
public void writeConstant() throws IOException {
    Term a = DefaultTermFactory.instance().createConstant("A");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DlgpWriter writer = new DlgpWriter(os);
    writer.write(new DefaultAtom(predicat, a));
    writer.flush();
    String s = new String(os.toByteArray(), "UTF-8");
    writer.close();
    char c = s.charAt(s.indexOf("(") + 1);
    Assert.assertTrue("Constant label does not begin with lower case.", Character.isLowerCase(c) || c == '<');
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

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