Search in sources :

Example 16 with Prefix

use of fr.lirmm.graphik.util.Prefix in project graal by graphik-team.

the class DlgpParser method parse.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE
// /////////////////////////////////////////////////////////////////////////
private static Object parse(String s) throws ParseException {
    InMemoryStream<Object> stream = new QueueStream<Object>();
    Producer p = new Producer(new StringReader(s), stream);
    try {
        p.run();
    } catch (Throwable t) {
        throw new DlgpParseException(t);
    }
    if (!stream.hasNext()) {
        throw new DlgpParseException("No statement found.");
    }
    Object o = null;
    do {
        o = stream.next();
    } while (o instanceof Directive || o instanceof Prefix);
    if (stream.hasNext()) {
        throw new DlgpParseException("Too much statements.");
    }
    stream.close();
    if (o instanceof ParseException) {
        throw (ParseException) o;
    }
    return o;
}
Also used : QueueStream(fr.lirmm.graphik.util.stream.QueueStream) StringReader(java.io.StringReader) Prefix(fr.lirmm.graphik.util.Prefix) ParseException(fr.lirmm.graphik.graal.api.io.ParseException)

Example 17 with Prefix

use of fr.lirmm.graphik.util.Prefix in project graal by graphik-team.

the class SparqlRuleTest method test0.

@Test
public void test0() throws IOException {
    String queryString = "PREFIX foaf:    <http://xmlns.com/foaf/0.1/> " + " PREFIX vcard:   <http://www.w3.org/2001/vcard-rdf/3.0#>                  " + "                                                                          " + " CONSTRUCT { ?x  vcard:N _:v .                                            " + "             _:v vcard:givenName ?gname .                                 " + "             _:v vcard:familyName ?fname }                                " + " WHERE                                                                    " + "  {                                                                       " + "     { ?x foaf:firstname ?gname }  . " + "     { ?x foaf:surname   ?fname }  . " + "  }";
    SparqlRuleParser parser = new SparqlRuleParser(queryString);
    StringWriter sw = new StringWriter();
    SparqlRuleWriter writer = new SparqlRuleWriter(sw);
    for (Prefix p : parser.getPrefixes()) writer.write(p);
    writer.write(parser.getRule());
    writer.close();
    String s = sw.toString();
    try {
        parser = new SparqlRuleParser(s);
    } catch (QueryParseException e) {
        Assert.assertFalse("QueryParseException", true);
    }
}
Also used : StringWriter(java.io.StringWriter) Prefix(fr.lirmm.graphik.util.Prefix) QueryParseException(com.hp.hpl.jena.query.QueryParseException) Test(org.junit.Test)

Example 18 with Prefix

use of fr.lirmm.graphik.util.Prefix in project graal by graphik-team.

the class SparqlConjunctiveQueryParser method execute.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void execute(String queryString) {
    this.prefixes = new LinkedList<Prefix>();
    List<Term> ans = new LinkedList<Term>();
    Query sparql = QueryFactory.create(queryString);
    for (Map.Entry<String, String> e : sparql.getPrefixMapping().getNsPrefixMap().entrySet()) {
        this.prefixes.add(new Prefix(e.getKey(), e.getValue()));
    }
    if (sparql.isSelectType()) {
        for (String v : sparql.getResultVars()) {
            ans.add(DefaultTermFactory.instance().createVariable(v));
        }
    }
    ElementVisitorImpl visitor = new ElementVisitorImpl(DefaultAtomSetFactory.instance().create());
    sparql.getQueryPattern().visit(visitor);
    // check if answer variables appear in the query body
    Set<Variable> bodyVars = visitor.getAtomSet().getVariables();
    for (Term t : ans) {
        if (t.isVariable() && !bodyVars.contains(t)) {
            throw new ParseError("The variable [" + t + "] of the answer list does not appear in the query body.");
        }
    }
    this.query = DefaultConjunctiveQueryFactory.instance().create(visitor.getAtomSet(), ans);
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Query(com.hp.hpl.jena.query.Query) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Prefix(fr.lirmm.graphik.util.Prefix) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) ParseError(fr.lirmm.graphik.graal.api.io.ParseError) Map(java.util.Map)

Example 19 with Prefix

use of fr.lirmm.graphik.util.Prefix in project graal by graphik-team.

the class SparqlRuleParser method execute.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void execute(String sparqlQuery) {
    Query sparql = QueryFactory.create(sparqlQuery);
    this.prefixes = new LinkedList<Prefix>();
    for (Map.Entry<String, String> e : sparql.getPrefixMapping().getNsPrefixMap().entrySet()) {
        this.prefixes.add(new Prefix(e.getKey(), e.getValue()));
    }
    Rule rule = new DefaultRule();
    if (sparql.isConstructType()) {
        Template template = sparql.getConstructTemplate();
        for (Triple triple : template.getTriples()) {
            rule.getHead().add(SparqlUtils.triple2Atom(triple));
        }
    }
    ElementVisitorImpl visitor = new ElementVisitorImpl(rule.getBody());
    sparql.getQueryPattern().visit(visitor);
    this.rule = rule;
}
Also used : DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) Triple(com.hp.hpl.jena.graph.Triple) Query(com.hp.hpl.jena.query.Query) Prefix(fr.lirmm.graphik.util.Prefix) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) Map(java.util.Map) Template(com.hp.hpl.jena.sparql.syntax.Template)

Example 20 with Prefix

use of fr.lirmm.graphik.util.Prefix in project graal by graphik-team.

the class OWL2ParserTest method partiallyIllegalTest.

@Test
public void partiallyIllegalTest() throws OWL2ParserException {
    OWL2Parser parser = new OWL2Parser(PREFIXES + " :A a owl:Class. " + ":B a owl:Class. " + ":C a owl:Class. " + ":p a owl:ObjectProperty. " + "[rdf:type owl:Restriction; " + "    owl:onProperty :p; " + "    owl:someValuesFrom [owl:unionOf ( [rdf:type owl:Restriction; " + "        owl:onProperty :p ; " + "        owl:allValuesFrom :A] :C ) ]" + "] rdfs:subClassOf :B .");
    int nbAssertion = 0;
    while (parser.hasNext()) {
        Object o = parser.next();
        if (!(o instanceof Prefix)) {
            ++nbAssertion;
        }
    }
    parser.close();
    Assert.assertEquals("Number of assertions found:", 1, nbAssertion);
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) Prefix(fr.lirmm.graphik.util.Prefix) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Test(org.junit.Test)

Aggregations

Prefix (fr.lirmm.graphik.util.Prefix)33 Test (org.junit.Test)27 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)23 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)17 Rule (fr.lirmm.graphik.graal.api.core.Rule)14 Atom (fr.lirmm.graphik.graal.api.core.Atom)6 Term (fr.lirmm.graphik.graal.api.core.Term)3 Map (java.util.Map)3 Query (com.hp.hpl.jena.query.Query)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)2 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)2 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)2 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)2 DefaultURI (fr.lirmm.graphik.util.DefaultURI)2 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)2 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Triple (com.hp.hpl.jena.graph.Triple)1 QueryParseException (com.hp.hpl.jena.query.QueryParseException)1 Template (com.hp.hpl.jena.sparql.syntax.Template)1