2ronwalf/*
2ronwalf * Created on Mar 30, 2005
2ronwalf */
2ronwalfpackage impl.jena;
2ronwalf
2ronwalfimport java.net.URI;
2ronwalfimport java.net.URISyntaxException;
2ronwalfimport java.util.ArrayList;
2ronwalfimport java.util.HashMap;
2ronwalfimport java.util.Iterator;
2ronwalfimport java.util.List;
2ronwalfimport java.util.Map;
2ronwalf
2ronwalfimport org.mindswap.exceptions.NotImplementedException;
2ronwalfimport org.mindswap.owl.OWLClass;
2ronwalfimport org.mindswap.owl.OWLDataProperty;
2ronwalfimport org.mindswap.owl.OWLDataValue;
2ronwalfimport org.mindswap.owl.OWLFactory;
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.OWLModel;
2ronwalfimport org.mindswap.owl.OWLObjectProperty;
2ronwalfimport org.mindswap.owl.OWLOntology;
2ronwalfimport org.mindswap.owl.OWLProperty;
2ronwalfimport org.mindswap.owl.vocabulary.SWRL;
2ronwalfimport org.mindswap.query.ABoxQuery;
2ronwalfimport org.mindswap.query.ABoxQueryParser;
2ronwalfimport org.mindswap.swrl.Atom;
2ronwalfimport org.mindswap.swrl.AtomList;
2ronwalfimport org.mindswap.swrl.BuiltinAtom;
2ronwalfimport org.mindswap.swrl.SWRLDataObject;
2ronwalfimport org.mindswap.swrl.SWRLDataValue;
2ronwalfimport org.mindswap.swrl.SWRLDataVariable;
2ronwalfimport org.mindswap.swrl.SWRLFactory;
2ronwalfimport org.mindswap.swrl.SWRLFactoryCreator;
2ronwalfimport org.mindswap.swrl.SWRLIndividual;
2ronwalfimport org.mindswap.swrl.SWRLIndividualObject;
2ronwalfimport org.mindswap.swrl.SWRLIndividualVariable;
2ronwalf
2ronwalfimport com.hp.hpl.jena.enhanced.EnhGraph;
2ronwalfimport com.hp.hpl.jena.graph.Node;
2ronwalfimport com.hp.hpl.jena.graph.Triple;
2ronwalfimport com.hp.hpl.jena.graph.query.Expression;
2ronwalfimport com.hp.hpl.jena.rdf.model.impl.LiteralImpl;
2ronwalfimport com.hp.hpl.jena.rdql.Query;
2ronwalfimport com.hp.hpl.jena.rdql.parser.ParsedLiteral;
2ronwalfimport com.hp.hpl.jena.rdql.parser.Q_Equal;
2ronwalfimport com.hp.hpl.jena.rdql.parser.Q_GreaterThan;
2ronwalfimport com.hp.hpl.jena.rdql.parser.Q_GreaterThanOrEqual;
2ronwalfimport com.hp.hpl.jena.rdql.parser.Q_LessThan;
2ronwalfimport com.hp.hpl.jena.rdql.parser.Q_LessThanOrEqual;
2ronwalfimport com.hp.hpl.jena.rdql.parser.Q_NotEqual;
2ronwalfimport com.hp.hpl.jena.vocabulary.RDF;
2ronwalf
2ronwalf/**
2ronwalf * @author Evren Sirin
2ronwalf *
2ronwalf */
2ronwalfpublic class RDQLParser implements ABoxQueryParser {
2ronwalf private static final String varNS = "var:";
2ronwalf
2ronwalf private OWLModel model;
2ronwalf
2ronwalf public RDQLParser(OWLModel model) {
2ronwalf this.model = model;
2ronwalf }
2ronwalf
2ronwalf public ABoxQuery parse( String rdqlString ) {
2ronwalf try {
2ronwalf OWLOntology ont = OWLFactory.createOntology();
2ronwalf SWRLFactory swrl = SWRLFactoryCreator.createFactory( ont );
2ronwalf List resultVars = new ArrayList();
2ronwalf Map vars = new HashMap();
2ronwalf
2ronwalf Query rdql = new Query( rdqlString );
2ronwalf AtomList atomList = swrl.createList();
2ronwalf for( Iterator i = rdql.getTriplePatterns().iterator(); i.hasNext(); ) {
2ronwalf Triple t = (Triple) i.next();
2ronwalf
2ronwalf if( t.getPredicate().isVariable() )
2ronwalf throw new IllegalArgumentException("Variables cannot be used in predicate position in AboxQuery");
2ronwalf
2ronwalf Atom atom = null;
2ronwalf URI pred = new URI( t.getPredicate().getURI() );
2ronwalf if( pred.toString().equals( RDF.type.getURI() ) ) {
2ronwalf if( t.getObject().isVariable() )
2ronwalf throw new IllegalArgumentException("Variables cannot be used as objects of rdf:type triples in ABoxQuery");
2ronwalf
2ronwalf OWLClass c = ont.createClass( new URI( t.getObject().getURI() ) );
2ronwalf SWRLIndividualObject arg = makeIndividalObject( t.getSubject(), ont );
2ronwalf
2ronwalf atom = swrl.createClassAtom( c, arg );
2ronwalf
2ronwalf vars.put(t.getSubject().toString(), arg);
2ronwalf }
2ronwalf else {
2ronwalf OWLProperty p = model.getProperty( pred );
2ronwalf
2ronwalf if( p == null )
2ronwalf throw new IllegalArgumentException( pred + " is not a known [Object|Data]Property." );
2ronwalf else if( p instanceof OWLDataProperty ) {
2ronwalf OWLDataProperty dp = ont.createDataProperty( p.getURI() ) ;
2ronwalf SWRLIndividualObject arg1 = makeIndividalObject( t.getSubject(), ont );
2ronwalf SWRLDataObject arg2 = makeDataObject( t.getObject(), ont );
2ronwalf
2ronwalf vars.put(t.getSubject().toString(), arg1);
2ronwalf vars.put(t.getObject().toString(), arg2);
2ronwalf
2ronwalf atom = swrl.createDataPropertyAtom( dp, arg1, arg2 );
2ronwalf }
2ronwalf else {
2ronwalf OWLObjectProperty op = ont.createObjectProperty( p.getURI() ) ;
2ronwalf SWRLIndividualObject arg1 = makeIndividalObject( t.getSubject(), ont );
2ronwalf SWRLIndividualObject arg2 = makeIndividalObject( t.getObject(), ont );
2ronwalf
2ronwalf vars.put(t.getSubject().toString(), arg1);
2ronwalf vars.put(t.getObject().toString(), arg2);
2ronwalf
2ronwalf atom = swrl.createIndividualPropertyAtom( op, arg1, arg2 );
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf atomList = atomList.add( atom );
2ronwalf }
2ronwalf
2ronwalf for( Iterator i = rdql.getConstraints().iterator(); i.hasNext(); ) {
2ronwalf Expression expr = (Expression) i.next();
2ronwalf
2ronwalf SWRLDataObject arg1 = makeDataObject( expr.getArg(0), ont) ;
2ronwalf SWRLDataObject arg2 = makeDataObject( expr.getArg(1), ont );
2ronwalf
2ronwalf BuiltinAtom atom = null;
2ronwalf if ( expr instanceof Q_Equal )
2ronwalf atom = swrl.createEqual( arg1, arg2 );
2ronwalf else if ( expr instanceof Q_NotEqual )
2ronwalf atom = swrl.createNotEqual( arg1, arg2 );
2ronwalf else if ( expr instanceof Q_GreaterThan )
2ronwalf atom = swrl.createGreaterThan( arg1, arg2 );
2ronwalf else if ( expr instanceof Q_GreaterThanOrEqual )
2ronwalf atom = swrl.createGreaterThanOrEqual( arg1, arg2 );
2ronwalf else if ( expr instanceof Q_LessThan )
2ronwalf atom = swrl.createLessThan( arg1, arg2 );
2ronwalf else if ( expr instanceof Q_LessThanOrEqual )
2ronwalf atom = swrl.createLessThanOrEqual( arg1, arg2 );
2ronwalf else
2ronwalf throw new NotImplementedException();
2ronwalf
2ronwalf atomList.add( atom );
2ronwalf }
2ronwalf
2ronwalf
2ronwalf for( Iterator i = rdql.getResultVars().iterator(); i.hasNext(); ) {
2ronwalf String var = (String) i.next();
2ronwalf
2ronwalf resultVars.add( vars.get( "?" + var ) );
2ronwalf }
2ronwalf
2ronwalf return new ABoxQuery( atomList, resultVars );
2ronwalf
2ronwalf } catch(URISyntaxException e) {
2ronwalf throw new IllegalArgumentException( e.getInput() + " is not a valid URI!");
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf private SWRLDataObject makeDataObject( Expression expr, OWLOntology ont ) throws URISyntaxException {
2ronwalf if( expr.isVariable() ) {
2ronwalf OWLIndividual ind = ont.createInstance( SWRL.Variable, new URI( varNS + expr.getName() ) );
2ronwalf return (SWRLDataObject) ind.castTo(SWRLDataVariable.class);
2ronwalf }
2ronwalf else if( expr.isConstant() ) {
2ronwalf OWLDataValue value = null;
2ronwalf if( expr instanceof ParsedLiteral ) {
2ronwalf ParsedLiteral lit = (ParsedLiteral) expr;
2ronwalf if( lit.isNode() ) {
2ronwalf throw new NotImplementedException();
2ronwalf }
2ronwalf else if( lit.isInt() ) {
2ronwalf value = ont.createDataValue( new Long( lit.getInt()) );
2ronwalf }
2ronwalf else if( lit.isDouble() ) {
2ronwalf value = ont.createDataValue( new Double( lit.getInt()) );
2ronwalf }
2ronwalf else if( lit.isBoolean() ) {
2ronwalf value = ont.createDataValue( lit.getBoolean() ? Boolean.TRUE : Boolean.FALSE );
2ronwalf }
2ronwalf else if( lit.isString() ) {
2ronwalf value = ont.createDataValue( lit.getString() );
2ronwalf }
2ronwalf else if( lit.isURI() ) {
2ronwalf value = ont.createDataValue( URI.create( lit.getURI() ) );
2ronwalf }
2ronwalf else
2ronwalf throw new IllegalArgumentException();
2ronwalf }
2ronwalf else
2ronwalf value = ont.createDataValue( expr.getValue() );
2ronwalf
2ronwalf return (SWRLDataValue) value.castTo(SWRLDataValue.class);
2ronwalf }
2ronwalf else {
2ronwalf throw new NotImplementedException();
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf private SWRLDataObject makeDataObject( Node node, OWLOntology ont ) throws URISyntaxException {
2ronwalf if( node.isVariable() ) {
2ronwalf OWLIndividual ind = ont.createInstance( SWRL.Variable, new URI( varNS + node.getName() ) );
2ronwalf return (SWRLDataObject) ind.castTo(SWRLDataVariable.class);
2ronwalf }
2ronwalf else {
2ronwalf OWLDataValue value = new OWLDataValueImpl( new LiteralImpl( node, (EnhGraph) ont.getImplementation() ) );
2ronwalf return (SWRLDataValue) value.castTo(SWRLDataValue.class);
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf private SWRLIndividualObject makeIndividalObject( Node node, OWLOntology ont ) throws URISyntaxException {
2ronwalf if( node.isVariable() ) {
2ronwalf OWLIndividual ind = ont.createInstance( SWRL.Variable, new URI( varNS + node.getName() ) );
2ronwalf return (SWRLIndividualObject) ind.castTo( SWRLIndividualVariable.class );
2ronwalf }
2ronwalf else {
2ronwalf OWLIndividual ind = ont.createInstance( org.mindswap.owl.vocabulary.OWL.Thing, new URI(
2ronwalf node.getURI() ) );
2ronwalf return (SWRLIndividualObject) ind.castTo( SWRLIndividual.class );
2ronwalf }
2ronwalf }
2ronwalf}