2ronwalf/*
2ronwalf * Created on Dec 28, 2004
2ronwalf */
2ronwalfpackage impl.swrl;
2ronwalf
2ronwalfimport java.util.HashSet;
2ronwalfimport java.util.Set;
2ronwalf
38daenzeroramaimport org.mindswap.exceptions.NotImplementedException;
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.OWLValue;
2ronwalfimport org.mindswap.owl.list.RDFList;
2ronwalfimport org.mindswap.owl.vocabulary.RDF;
2ronwalfimport org.mindswap.owl.vocabulary.SWRL;
2ronwalfimport org.mindswap.owl.vocabulary.SWRLB;
38daenzeroramaimport org.mindswap.query.ValueMap;
2ronwalfimport org.mindswap.swrl.BuiltinAtom;
2ronwalfimport org.mindswap.swrl.SWRLDataObject;
2ronwalfimport org.mindswap.swrl.SWRLObject;
2ronwalf
2ronwalf/**
2ronwalf * @author Evren Sirin
2ronwalf *
2ronwalf */
2ronwalfpublic class BuiltinAtomImpl extends AtomImpl implements BuiltinAtom {
2ronwalf private final static Set THREE_ARGS = new HashSet();
2ronwalf static {
2ronwalf THREE_ARGS.add( SWRLB.add );
2ronwalf THREE_ARGS.add( SWRLB.subtract );
2ronwalf THREE_ARGS.add( SWRLB.multiply );
2ronwalf THREE_ARGS.add( SWRLB.divide );
2ronwalf }
2ronwalf
2ronwalf public BuiltinAtomImpl(OWLIndividual ind) {
2ronwalf super(ind);
2ronwalf }
2ronwalf
2ronwalf public OWLIndividual getBuiltin() {
2ronwalf return getProperty(SWRL.builtin);
2ronwalf }
2ronwalf
2ronwalf public void setBuiltin(OWLIndividual builtin) {
2ronwalf setProperty(SWRL.builtin, builtin);
2ronwalf }
2ronwalf
2ronwalf public int getArgumentCount() {
2ronwalf if( THREE_ARGS.contains( getBuiltin() ) )
2ronwalf return 3;
2ronwalf
2ronwalf return 2;
2ronwalf }
2ronwalf
2ronwalf private RDFList getArguments() {
2ronwalf OWLIndividual args = getProperty(SWRL.arguments);
2ronwalf
2ronwalf return (args == null) ? null : (RDFList) args.castTo( RDFList.class );
2ronwalf }
2ronwalf
2ronwalf public SWRLObject getArgument( int index ) {
2ronwalf RDFList args = getArguments();
2ronwalf
2ronwalf if( args == null ) return null;
2ronwalf
2ronwalf OWLValue arg = args.getValue( index );
2ronwalf
2ronwalf if( arg == null ) return null;
2ronwalf
2ronwalf return (SWRLDataObject) arg.castTo( SWRLDataObject.class );
2ronwalf }
2ronwalf
2ronwalf public void addArgument( SWRLObject arg ) {
2ronwalf if(!(arg instanceof SWRLDataObject))
2ronwalf throw new IllegalArgumentException("BuiltinAtom argument should be a SWRLDataObject");
2ronwalf
2ronwalf RDFList args = getArguments();
2ronwalf
2ronwalf if( args == null ) {
2ronwalf RDFList nil = getOntology().createList( RDF.ListVocabulary );
2ronwalf args = nil.insert( arg );
2ronwalf setProperty(SWRL.arguments, args);
2ronwalf }
2ronwalf else
2ronwalf args.add( arg );
2ronwalf }
2ronwalf
2ronwalf public void setArgument(int index, SWRLObject arg) {
2ronwalf if(!(arg instanceof SWRLDataObject))
2ronwalf throw new IllegalArgumentException("BuiltinAtom argument should be a SWRLDataObject");
2ronwalf
2ronwalf RDFList args = getArguments();
2ronwalf
2ronwalf if( args == null ) {
2ronwalf if( index == 0 ) {
2ronwalf RDFList nil = getOntology().createList( RDF.ListVocabulary );
2ronwalf args = nil.insert( arg );
2ronwalf setProperty(SWRL.arguments, args);
2ronwalf }
2ronwalf else
2ronwalf throw new IndexOutOfBoundsException();
2ronwalf }
2ronwalf else {
2ronwalf args.set( index, arg );
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf public String toString() {
2ronwalf OWLIndividual builtin = getBuiltin();
2ronwalf if(builtin == null)
2ronwalf return "Missing_class_predicate";
2ronwalf
2ronwalf StringBuffer str = new StringBuffer();
2ronwalf if( builtin.equals( SWRLB.equal ) ) {
2ronwalf str.append( "(" );
2ronwalf str.append( getArgument(0) );
2ronwalf str.append( " = " );
2ronwalf str.append( getArgument(1) );
2ronwalf str.append( ")" );
2ronwalf }
2ronwalf else if( builtin.equals( SWRLB.lessThan ) ) {
2ronwalf str.append( "(" );
2ronwalf str.append( getArgument(0) );
2ronwalf str.append( " < " );
2ronwalf str.append( getArgument(1) );
2ronwalf str.append( ")" );
2ronwalf }
2ronwalf else if( builtin.equals( SWRLB.greaterThan ) ) {
2ronwalf str.append( "(" );
2ronwalf str.append( getArgument(0) );
2ronwalf str.append( " > " );
2ronwalf str.append( getArgument(1) );
2ronwalf str.append( ")" );
2ronwalf }
2ronwalf else if( builtin.equals( SWRLB.add ) ) {
2ronwalf str.append( "(" );
2ronwalf str.append( getArgument(0) );
2ronwalf str.append( " = " );
2ronwalf str.append( getArgument(1) );
2ronwalf str.append( " + " );
2ronwalf str.append( getArgument(2) );
2ronwalf str.append( ")" );
2ronwalf }
2ronwalf else if( builtin.equals( SWRLB.subtract ) ) {
2ronwalf str.append( "(" );
2ronwalf str.append( getArgument(0) );
2ronwalf str.append( " = " );
2ronwalf str.append( getArgument(1) );
2ronwalf str.append( " - " );
2ronwalf str.append( getArgument(2) );
2ronwalf str.append( ")" );
2ronwalf }
2ronwalf else {
2ronwalf str.append( builtin.getLocalName() ).append( "(" );
2ronwalf for(int i = 0; i < getArgumentCount(); i++) {
2ronwalf SWRLObject arg = getArgument( i );
2ronwalf if( i > 0 )
2ronwalf str.append( ", " );
2ronwalf str.append( arg );
2ronwalf }
2ronwalf str.append(")");
2ronwalf }
2ronwalf
2ronwalf return str.toString();
2ronwalf }
38daenzerorama
38daenzerorama public void evaluate(ValueMap values) {
38daenzerorama OWLIndividual builtin = getBuiltin();
38daenzerorama if (! (builtin.equals(SWRLB.add) || builtin.equals(SWRLB.subtract) ||
38daenzerorama builtin.equals(SWRLB.multiply) || builtin.equals(SWRLB.divide)))
38daenzerorama return;
38daenzerorama
38daenzerorama throw new NotImplementedException("Builtin SWRL atoms cannot be evaluated");
38daenzerorama }
2ronwalf}