// Make sure to resolve the query parameterization first, i.e.
// substitute the variables with initial bindings, if applicable
triples = new LinkedHashSet<Triple>( resolveParameterization( basicPattern ) );
final Query query = new QueryImpl( kb, isDistinct );
for( Iterator<?> i = resultVars.iterator(); i.hasNext(); ) {
String var = (String) i.next();
query.addResultVar( ATermUtils.makeVar( var ) );
}
for( final Triple t : new ArrayList<Triple>( triples ) ) {
if( !triples.contains( t ) ) {
continue;
}
Node subj = t.getSubject();
Node pred = t.getPredicate();
Node obj = t.getObject();
if (BuiltinTerm.isSyntax(pred) || BuiltinTerm.isSyntax(obj))
continue;
cache( subj );
cache( pred );
cache( obj );
}
final Set<ATermAppl> possibleLiteralVars = new HashSet<ATermAppl>();
//throw exception if triples is empty
if(triples.isEmpty()){
throw new UnsupportedQueryException(
"Empty BGT" );
}
for( final Triple t : triples ) {
Node subj = t.getSubject();
Node pred = t.getPredicate();
Node obj = t.getObject();
ATermAppl s = (ATermAppl) terms.get( subj );
ATermAppl p = (ATermAppl) terms.get( pred );
ATermAppl o = (ATermAppl) terms.get( obj );
if( pred.equals( RDF.Nodes.type ) ) {
// Map ?c rdf:type owl:Class to SubClassOf(?c owl:Thing)
if( obj.equals( OWL.Class.asNode() ) ) {
query.add( QueryAtomFactory.SubClassOfAtom( s, TermFactory.TOP ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.CLASS );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
}
//NamedIndividual(p)
else if( obj.equals( OWL2.NamedIndividual.asNode() ) ) {
query.add( QueryAtomFactory.TypeAtom(s, TermFactory.TOP ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.CLASS );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
}
// ObjectProperty(p)
else if( obj.equals( OWL.ObjectProperty.asNode() ) ) {
query.add( QueryAtomFactory.ObjectPropertyAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// DatatypeProperty(p)
else if( obj.equals( OWL.DatatypeProperty.asNode() ) ) {
query.add( QueryAtomFactory.DatatypePropertyAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// Property(p)
else if( obj.equals( RDF.Property.asNode() ) ) {
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// Functional(p)
else if( obj.equals( OWL.FunctionalProperty.asNode() ) ) {
query.add( QueryAtomFactory.FunctionalAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// InverseFunctional(p)
else if( obj.equals( OWL.InverseFunctionalProperty.asNode() ) ) {
query.add( QueryAtomFactory.InverseFunctionalAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// Transitive(p)
else if( obj.equals( OWL.TransitiveProperty.asNode() ) ) {
query.add( QueryAtomFactory.TransitiveAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// Symmetric(p)
else if( obj.equals( OWL.SymmetricProperty.asNode() ) ) {
query.add( QueryAtomFactory.SymmetricAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// Asymmetric(p)
else if( obj.equals( OWL2.AsymmetricProperty.asNode() ) ) {
query.add( QueryAtomFactory.AsymmetricAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// Reflexive(p)
else if( obj.equals( OWL2.ReflexiveProperty.asNode() ) ) {
query.add( QueryAtomFactory.ReflexiveAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// Irreflexive(p)
else if( obj.equals( OWL2.IrreflexiveProperty.asNode() ) ) {
query.add( QueryAtomFactory.IrreflexiveAtom( s ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
else {
ensureTypedProperty( s );
}
}
// Annotation(s,pa,o)
else if( hasObject( pred, RDF.type.asNode(), OWL.AnnotationProperty.asNode() ) ) {
query.add( QueryAtomFactory.AnnotationAtom( s, p, o ) );
if( ATermUtils.isVar( s ) || ATermUtils.isVar( p ) || ATermUtils.isVar( o ) ) {
throw new UnsupportedQueryException(
"Variables in annotation atom are not supported." );
}
else {
ensureTypedProperty( p );
}
}
// Type(i,c)
else {
query.add( QueryAtomFactory.TypeAtom( s, o ) );
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.CLASS );
}
else if( !kb.isClass( o ) ) {
if( log.isLoggable( Level.FINE ) )
log
.fine( "Class " + o
+ " used in the query is not defined in the KB." );
}
if( isDistinguishedVariable( subj ) ) {
query.addDistVar( s, VarType.INDIVIDUAL );
}
}
}
// SameAs(i1,i2)
else if( pred.equals( OWL.sameAs.asNode() ) ) {
query.add( QueryAtomFactory.SameAsAtom( s, o ) );
if( isDistinguishedVariable( subj ) ) {
query.addDistVar( s, VarType.INDIVIDUAL );
}
if( isDistinguishedVariable( obj ) ) {
query.addDistVar( o, VarType.INDIVIDUAL );
}
}
// DifferentFrom(i1,i2)
else if( pred.equals( OWL.differentFrom.asNode() ) ) {
query.add( QueryAtomFactory.DifferentFromAtom( s, o ) );
if( isDistinguishedVariable( subj ) ) {
query.addDistVar( s, VarType.INDIVIDUAL );
}
if( isDistinguishedVariable( obj ) ) {
query.addDistVar( o, VarType.INDIVIDUAL );
}
}
// SubClassOf(c1,c2)
else if( pred.equals( RDFS.subClassOf.asNode() ) ) {
query.add( QueryAtomFactory.SubClassOfAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.CLASS );
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.CLASS );
}
}
// strict subclass - nonmonotonic
else if( pred.equals( SparqldlExtensionsVocabulary.strictSubClassOf.asNode() ) ) {
query.add( QueryAtomFactory.StrictSubClassOfAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.CLASS );
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.CLASS );
}
}
// direct subclass - nonmonotonic
else if( pred.equals( SparqldlExtensionsVocabulary.directSubClassOf.asNode() ) ) {
query.add( QueryAtomFactory.DirectSubClassOfAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.CLASS );
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.CLASS );
}
}
// EquivalentClass(c1,c2)
else if( pred.equals( OWL.equivalentClass.asNode() ) ) {
query.add( QueryAtomFactory.EquivalentClassAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.CLASS );
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.CLASS );
}
}
// DisjointWith(c1,c2)
else if( pred.equals( OWL.disjointWith.asNode() ) ) {
query.add( QueryAtomFactory.DisjointWithAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.CLASS );
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.CLASS );
}
}
// ComplementOf(c1,c2)
else if( pred.equals( OWL.complementOf.asNode() ) ) {
query.add( QueryAtomFactory.ComplementOfAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.CLASS );
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.CLASS );
}
}
// propertyDisjointWith(p1,p2)
else if( pred.equals( OWL2.propertyDisjointWith.asNode() ) ) {
ensureTypedProperty( s );
ensureTypedProperty( o );
query.add( QueryAtomFactory.PropertyDisjointWithAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( o );
variableSubjects.add( o );
}
}
}
// SubPropertyOf(p1,p2)
else if( pred.equals( RDFS.subPropertyOf.asNode() ) ) {
ensureTypedProperty( s );
ensureTypedProperty( o );
query.add( QueryAtomFactory.SubPropertyOfAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( o );
variableSubjects.add( o );
}
}
}
// DirectSubPropertyOf(i,p) - nonmonotonic
else if( pred.equals( SparqldlExtensionsVocabulary.directSubPropertyOf.asNode() ) ) {
ensureTypedProperty( s );
ensureTypedProperty( o );
query.add( QueryAtomFactory.DirectSubPropertyOfAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( o );
variableSubjects.add( o );
}
}
}
// StrictSubPropertyOf(i,p) - nonmonotonic
else if( pred.equals( SparqldlExtensionsVocabulary.strictSubPropertyOf.asNode() ) ) {
ensureTypedProperty( s );
ensureTypedProperty( o );
query.add( QueryAtomFactory.StrictSubPropertyOfAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( o );
variableSubjects.add( o );
}
}
}
// EquivalentProperty(p1,p2)
else if( pred.equals( OWL.equivalentProperty.asNode() ) ) {
ensureTypedProperty( s );
ensureTypedProperty( o );
query.add( QueryAtomFactory.EquivalentPropertyAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( o );
variableSubjects.add( o );
}
}
}
// Domain(p1, c)
else if( pred.equals( RDFS.domain.asNode() ) ) {
ensureTypedProperty( s );
query.add( QueryAtomFactory.DomainAtom( s, o ) );
if ( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( s, VarType.CLASS );
}
}
// Range(p1, c)
else if( pred.equals( RDFS.range.asNode() ) ) {
ensureTypedProperty( s );
query.add( QueryAtomFactory.RangeAtom( s, o ) );
if ( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
// TODO it could also range over datatypes.
query.addDistVar( s, VarType.CLASS );
}
}
// InverseOf(p1,p2)
else if( pred.equals( OWL.inverseOf.asNode() ) ) {
ensureTypedProperty( s );
ensureTypedProperty( o );
query.add( QueryAtomFactory.InverseOfAtom( s, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( o );
variableSubjects.add( o );
}
}
}
// DirectType(i,c) - nonmonotonic
else if( pred.equals( SparqldlExtensionsVocabulary.directType.asNode() ) ) {
query.add( QueryAtomFactory.DirectTypeAtom( s, o ) );
if( isDistinguishedVariable( subj ) ) {
query.addDistVar( s, VarType.INDIVIDUAL );
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.CLASS );
}
}
else if( kb.isAnnotationProperty( p ) ) {
if( !PelletOptions.USE_ANNOTATION_SUPPORT ) {
throw new UnsupportedQueryException(
"Cannot answer annotation queries when PelletOptions.USE_ANNOTATION_SUPPORT is false!" );
}
query.add( QueryAtomFactory.AnnotationAtom( s, p, o ) );
if( ATermUtils.isVar( s ) ) {
ensureDistinguished( subj );
query.addDistVar( s, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( s );
variableSubjects.add( s );
}
}
if( ATermUtils.isVar( o ) ) {
ensureDistinguished( obj );
query.addDistVar( o, VarType.PROPERTY );
if( handleVariableSPO ) {
variablePredicates.remove( o );
variableSubjects.add( o );
}
}
// throw new UnsupportedQueryException(
// "Annotation properties are not supported in queries." );
}
// PropertyValue(i,p,j)
else {
if( s == null || p == null || o == null ) {
throw new UnsupportedQueryException("Atom conversion incomplete for: " + t);
}
ensureTypedProperty( p );
query.add( QueryAtomFactory.PropertyValueAtom( s, p, o ) );
if( ATermUtils.isVar( p ) ) {
ensureDistinguished( pred );
query.addDistVar( p, VarType.PROPERTY );
// If the predicate is a variable used in a subject position
// we don't have to consider it as it is bound to another
// triple pattern
if( !variableSubjects.contains( p ) )
variablePredicates.add( p );
}
if( isDistinguishedVariable( subj ) ) {
query.addDistVar( s, VarType.INDIVIDUAL );
}
if( isDistinguishedVariable( obj ) ) {
if( ATermUtils.isVar( p ) ) {
possibleLiteralVars.add( o );
}
else {
if( kb.isObjectProperty( p ) ) {
query.addDistVar( o, VarType.INDIVIDUAL );
}
else if( kb.isDatatypeProperty( p ) ) {
query.addDistVar( o, VarType.LITERAL );
}
}
}
}
}
for( final ATermAppl v : possibleLiteralVars ) {
if( !query.getDistVars().contains( v ) ) {
query.addDistVar( v, VarType.LITERAL );
}
query.addDistVar( v, VarType.INDIVIDUAL );
}
if( !handleVariableSPO )
return query;