Search in sources :

Example 6 with Predicate

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

the class DlgpWriterTest method writePredicate.

@Test
public void writePredicate() throws IOException {
    Predicate p = new Predicate("P", 1);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DlgpWriter writer = new DlgpWriter(os);
    writer.write(new DefaultAtom(p, cst));
    writer.flush();
    String s = new String(os.toByteArray(), "UTF-8");
    writer.close();
    Character c = s.charAt(0);
    Assert.assertTrue("Predicate label does not begin with lower case or double quote.", Character.isLowerCase(c) || c == '<');
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 7 with Predicate

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

the class DlgpWriterTest method writeURIForbiddenChars.

@Test
public void writeURIForbiddenChars() throws IOException {
    Predicate p = new Predicate(new DefaultURI("/< >\"{}|^`\\/"), 1);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DlgpWriter writer = new DlgpWriter(os);
    writer.write(new DefaultAtom(p, cst));
    writer.flush();
    String s = new String(os.toByteArray(), "UTF-8");
    writer.close();
    System.out.println(s);
    Assert.assertEquals("</\\u003c\\u0020\\u003e\\u0022\\u007b\\u007d\\u007c\\u005e\\u0060\\u005c/>(<A>).\n", s);
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultURI(fr.lirmm.graphik.util.DefaultURI) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 8 with Predicate

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

the class DlgpWriterTest method bug76.

@Test
public void bug76() throws IOException {
    Prefix p1 = new Prefix("a", "http://p#");
    Prefix p2 = new Prefix("b", "http://p#p/");
    Predicate p = new Predicate(new DefaultURI("http://p#p/toto"), 1);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DlgpWriter writer = new DlgpWriter(os);
    writer.write(p1);
    writer.write(p2);
    writer.write(new DefaultAtom(p, cst));
    writer.flush();
    String s = new String(os.toByteArray(), "UTF-8");
    writer.close();
    Assert.assertTrue(s.contains("b:toto(<A>)."));
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Prefix(fr.lirmm.graphik.util.Prefix) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultURI(fr.lirmm.graphik.util.DefaultURI) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 9 with Predicate

use of fr.lirmm.graphik.graal.api.core.Predicate 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 10 with Predicate

use of fr.lirmm.graphik.graal.api.core.Predicate 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)

Aggregations

Predicate (fr.lirmm.graphik.graal.api.core.Predicate)77 Atom (fr.lirmm.graphik.graal.api.core.Atom)35 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)28 Term (fr.lirmm.graphik.graal.api.core.Term)27 Test (org.junit.Test)25 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)16 LinkedList (java.util.LinkedList)16 Theory (org.junit.experimental.theories.Theory)14 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)9 Rule (fr.lirmm.graphik.graal.api.core.Rule)9 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)9 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)7 DefaultURI (fr.lirmm.graphik.util.DefaultURI)7 TreeSet (java.util.TreeSet)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)6 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)6 Variable (fr.lirmm.graphik.graal.api.core.Variable)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 Pair (org.apache.commons.lang3.tuple.Pair)6