Search in sources :

Example 41 with DefaultAtom

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

the class IDConditionImpl method generateRule.

@Override
public Rule generateRule(Predicate bodyPredicate, Predicate headPredicate) {
    List<Term> body = new ArrayList<Term>();
    List<Term> head = new LinkedList<Term>();
    // initialize body with fresh variable
    for (int i = 0; i < condBody.length; i++) body.add(DefaultTermFactory.instance().createVariable("X" + condBody[i]));
    // pick frontier variables from the head
    for (int i = 0; i < this.condHead.length; i++) {
        head.add(DefaultTermFactory.instance().createVariable("X" + condHead[i]));
    }
    Rule r = DefaultRuleFactory.instance().create();
    r.getBody().add(new DefaultAtom(bodyPredicate, body));
    r.getHead().add(new DefaultAtom(headPredicate, head));
    return r;
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ArrayList(java.util.ArrayList) Term(fr.lirmm.graphik.graal.api.core.Term) Rule(fr.lirmm.graphik.graal.api.core.Rule) LinkedList(java.util.LinkedList)

Example 42 with DefaultAtom

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

the class HierarchicalCompilation method getRewritingOf.

@Override
public Collection<Pair<Atom, Substitution>> getRewritingOf(Atom father) {
    LinkedList<Pair<Atom, Substitution>> res = new LinkedList<Pair<Atom, Substitution>>();
    res.add(new ImmutablePair<Atom, Substitution>(father, Substitutions.emptySubstitution()));
    Integer index = predicateIndex.get(father.getPredicate());
    if (index != null)
        for (int i = 0; i < sizeOrder; i++) {
            if (order[index][i] == 1) {
                Atom a = new DefaultAtom(father);
                a.setPredicate(indexPredicate.get(i));
                res.add(new ImmutablePair<Atom, Substitution>(a, Substitutions.emptySubstitution()));
            }
        }
    return res;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) LinkedList(java.util.LinkedList) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Example 43 with DefaultAtom

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

the class AtomSetUtils method union.

public static InMemoryAtomSet union(InMemoryAtomSet a1, InMemoryAtomSet a2) {
    InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create();
    CloseableIteratorWithoutException<Atom> it = a1.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        atomset.add(new DefaultAtom(a));
    }
    it = a2.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (!atomset.contains(a)) {
            atomset.add(new DefaultAtom(a));
        }
    }
    return atomset;
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 44 with DefaultAtom

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

the class AbstractRDFListener method handleStatement.

@Override
public void handleStatement(Statement st) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(st.toString());
    }
    Predicate predicate = new Predicate(new DefaultURI(st.getPredicate().toString()), 2);
    Term subject = DefaultTermFactory.instance().createConstant(new DefaultURI(st.getSubject().toString()));
    Term object;
    fr.lirmm.graphik.util.URI datatype;
    if (st.getObject() instanceof Literal) {
        Literal l = (Literal) st.getObject();
        if (l.getDatatype() == null) {
            datatype = URIUtils.RDF_LANG_STRING;
        } else {
            datatype = new fr.lirmm.graphik.util.DefaultURI(l.getDatatype().getNamespace(), l.getDatatype().getLocalName());
        }
        String value = l.getLabel();
        if (datatype.equals(URIUtils.RDF_LANG_STRING)) {
            value += "@" + l.getLanguage();
        }
        object = DefaultTermFactory.instance().createLiteral(datatype, value);
    } else {
        object = DefaultTermFactory.instance().createConstant(new DefaultURI(st.getObject().toString()));
    }
    DefaultAtom a = new DefaultAtom(predicate, subject, object);
    this.createAtom(a);
}
Also used : DefaultURI(fr.lirmm.graphik.util.DefaultURI) Literal(org.eclipse.rdf4j.model.Literal) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultURI(fr.lirmm.graphik.util.DefaultURI) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 45 with DefaultAtom

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

the class AtomTest method equalsTest.

@Test
public void equalsTest() {
    Predicate predicate = new Predicate("pred", 3);
    Term[] terms = new Term[3];
    terms[0] = new DefaultVariable("X");
    terms[1] = new DefaultConstant("a");
    terms[2] = new DefaultConstant("b");
    Atom atom = new DefaultAtom(predicate, Arrays.asList(terms));
    Assert.assertTrue("Atom not equals itself", atom.equals(atom));
    Assert.assertTrue("Atom not equals it clone", atom.equals(new DefaultAtom(atom)));
    Predicate otherPred = new Predicate("otherPred", 3);
    Term[] otherTerms = new Term[3];
    otherTerms[0] = new DefaultVariable("Y");
    otherTerms[1] = new DefaultConstant("b");
    otherTerms[2] = new DefaultConstant("b");
    Atom other = new DefaultAtom(otherPred, Arrays.asList(terms));
    Assert.assertFalse("Atom equals an other atom with other predicate", atom.equals(other));
    other = new DefaultAtom(predicate, Arrays.asList(otherTerms));
    Assert.assertFalse("Atom equals an other atom with other terms", atom.equals(other));
    other = new DefaultAtom(atom);
    other.setPredicate(otherPred);
    Assert.assertFalse("Atom equals a copy with modified predicate", predicate.equals(other));
    other = new DefaultAtom(atom);
    other.setTerm(2, terms[0]);
    Assert.assertFalse("Atom equals a copy with modified terms", atom.equals(other));
}
Also used : 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) Test(org.junit.Test)

Aggregations

DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)45 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)25 Term (fr.lirmm.graphik.graal.api.core.Term)25 Atom (fr.lirmm.graphik.graal.api.core.Atom)21 Test (org.junit.Test)20 LinkedList (java.util.LinkedList)17 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)12 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)7 Rule (fr.lirmm.graphik.graal.api.core.Rule)7 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)7 Variable (fr.lirmm.graphik.graal.api.core.Variable)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 Constant (fr.lirmm.graphik.graal.api.core.Constant)5 DefaultURI (fr.lirmm.graphik.util.DefaultURI)5 Pair (org.apache.commons.lang3.tuple.Pair)5 DefaultRule (fr.lirmm.graphik.graal.core.DefaultRule)4 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)4 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)3