2ronwalf/*
2ronwalf * Created on Dec 28, 2004
2ronwalf */
2ronwalfpackage impl.swrl;
2ronwalf
2ronwalf
2ronwalfimport impl.owl.list.RDFListImpl;
2ronwalf
2ronwalfimport java.util.ArrayList;
2ronwalfimport java.util.List;
2ronwalf
2ronwalfimport org.mindswap.exceptions.NotImplementedException;
2ronwalfimport org.mindswap.owl.OWLDataValue;
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.OWLOntology;
2ronwalfimport org.mindswap.owl.OWLValue;
2ronwalfimport org.mindswap.owl.list.RDFList;
2ronwalfimport org.mindswap.owl.vocabulary.SWRL;
2ronwalfimport org.mindswap.owl.vocabulary.SWRLB;
2ronwalfimport org.mindswap.query.ABoxQuery;
2ronwalfimport org.mindswap.query.ValueMap;
2ronwalfimport org.mindswap.swrl.Atom;
2ronwalfimport org.mindswap.swrl.AtomList;
2ronwalfimport org.mindswap.swrl.BuiltinAtom;
2ronwalfimport org.mindswap.swrl.ClassAtom;
2ronwalfimport org.mindswap.swrl.DataPropertyAtom;
2ronwalfimport org.mindswap.swrl.DifferentIndividualsAtom;
2ronwalfimport org.mindswap.swrl.IndividualPropertyAtom;
2ronwalfimport org.mindswap.swrl.SWRLDataObject;
2ronwalfimport org.mindswap.swrl.SWRLDataValue;
2ronwalfimport org.mindswap.swrl.SWRLFactory;
2ronwalfimport org.mindswap.swrl.SWRLFactoryCreator;
2ronwalfimport org.mindswap.swrl.SWRLIndividual;
2ronwalfimport org.mindswap.swrl.SWRLIndividualObject;
2ronwalfimport org.mindswap.swrl.SWRLObject;
2ronwalfimport org.mindswap.swrl.SameIndividualAtom;
2ronwalfimport org.mindswap.swrl.Variable;
2ronwalf
2ronwalf/**
2ronwalf * @author Evren Sirin
2ronwalf *
2ronwalf */
2ronwalfpublic class AtomListImpl extends RDFListImpl implements AtomList {
2ronwalf public AtomListImpl(OWLIndividual ind) {
2ronwalf super(ind);
2ronwalf
2ronwalf setVocabulary( SWRL.AtomListVocabulary );
2ronwalf }
2ronwalf
2ronwalf public AtomList add( Atom item ) {
2ronwalf if( isEmpty() )
2ronwalf return insert( item );
2ronwalf
2ronwalf RDFList rest = getRest();
2ronwalf
2ronwalf if(rest.isEmpty())
2ronwalf setRest( rest.insert( item ) );
2ronwalf else
2ronwalf rest.add(item);
2ronwalf
2ronwalf return this;
2ronwalf }
2ronwalf
2ronwalf public AtomList insert( Atom first ) {
2ronwalf OWLOntology ont = getOntology();
38daenzerorama AtomListImpl list = new AtomListImpl( ont.createInstance( vocabulary.List() ) );
38daenzerorama
2ronwalf list.setFirst( first );
2ronwalf list.setRest( this );
2ronwalf
2ronwalf return list;
2ronwalf }
2ronwalf
2ronwalf public AtomList insertAt(int index, Atom item) {
2ronwalf return (AtomList) super.insertAt( index, item).castTo( AtomList.class );
2ronwalf }
2ronwalf
2ronwalf public RDFList insert( OWLValue item ) {
2ronwalf Atom atom = (Atom) item.castTo( Atom.class );
2ronwalf
2ronwalf return add( atom );
2ronwalf }
2ronwalf
2ronwalf public OWLValue getFirstValue() {
2ronwalf return (Atom) getProperty(vocabulary.first()).castTo(Atom.class);
2ronwalf }
2ronwalf
2ronwalf public RDFList getRest() {
2ronwalf return (RDFList) getProperty(vocabulary.rest()).castTo(AtomList.class);
2ronwalf }
2ronwalf
2ronwalf public Atom atomAt(int index) {
2ronwalf return (Atom) get(index);
2ronwalf }
2ronwalf
2ronwalf public ABoxQuery toQuery() {
2ronwalf return new ABoxQuery(this);
2ronwalf }
2ronwalf
2ronwalf public ABoxQuery toQuery(List resultVars) {
2ronwalf return new ABoxQuery(this, resultVars);
2ronwalf }
2ronwalf
2ronwalf public SWRLIndividualObject getIndidividualObject(SWRLIndividualObject arg, ValueMap binding) {
2ronwalf if( arg.isVariable() && binding.hasValue( (Variable) arg ) ) {
2ronwalf OWLIndividual ind = (OWLIndividual) binding.getValue( (Variable) arg );
2ronwalf arg = (SWRLIndividual) ind.castTo(SWRLIndividual.class);
2ronwalf }
2ronwalf
2ronwalf return arg;
2ronwalf }
2ronwalf
2ronwalf public SWRLDataObject getDataObject(SWRLDataObject arg, ValueMap binding) {
2ronwalf if( arg.isVariable() && binding.hasValue( (Variable) arg ) ) {
2ronwalf OWLDataValue dv = (OWLDataValue) binding.getValue( (Variable) arg );
2ronwalf arg = (SWRLDataValue) dv.castTo(SWRLDataValue.class);
2ronwalf }
2ronwalf
2ronwalf return arg;
2ronwalf }
2ronwalf
38daenzerorama public void evaluate(ValueMap values) {
38daenzerorama for (int i = 0; i < size(); i++)
38daenzerorama atomAt(i).evaluate(values);
38daenzerorama }
38daenzerorama
2ronwalf public AtomList apply( ValueMap binding ) {
2ronwalf OWLOntology ont = getOntology();
2ronwalf SWRLFactory swrl = SWRLFactoryCreator.createFactory( ont );
2ronwalf
2ronwalf AtomList newList = swrl.createList();
2ronwalf for( AtomList list = this; !list.isEmpty(); list = (AtomList) list.getRest() ) {
2ronwalf Atom atom = (Atom) list.getFirst();
2ronwalf
2ronwalf Atom newAtom = null;
2ronwalf if(atom instanceof ClassAtom) {
2ronwalf ClassAtom classAtom = (ClassAtom) atom;
2ronwalf newAtom = swrl.createClassAtom(
2ronwalf classAtom.getClassPredicate(),
2ronwalf getIndidividualObject( classAtom.getArgument1(), binding ) );
2ronwalf }
2ronwalf else if(atom instanceof IndividualPropertyAtom) {
2ronwalf IndividualPropertyAtom indPropAtom = (IndividualPropertyAtom) atom;
2ronwalf newAtom = swrl.createIndividualPropertyAtom(
2ronwalf indPropAtom.getPropertyPredicate(),
2ronwalf getIndidividualObject( indPropAtom.getArgument1(), binding ),
2ronwalf getIndidividualObject( indPropAtom.getArgument2(), binding ));
2ronwalf }
2ronwalf else if(atom instanceof DataPropertyAtom) {
2ronwalf DataPropertyAtom dataPropAtom = (DataPropertyAtom) atom;
2ronwalf newAtom = swrl.createDataPropertyAtom(
2ronwalf dataPropAtom.getPropertyPredicate(),
2ronwalf getIndidividualObject( dataPropAtom.getArgument1(), binding ),
2ronwalf getDataObject( dataPropAtom.getArgument2(), binding ));
2ronwalf }
2ronwalf else if(atom instanceof SameIndividualAtom) {
2ronwalf SameIndividualAtom sameAtom = (SameIndividualAtom) atom;
2ronwalf newAtom = swrl.createSameIndividualAtom(
2ronwalf getIndidividualObject( sameAtom.getArgument1(), binding ),
2ronwalf getIndidividualObject( sameAtom.getArgument2(), binding ));
2ronwalf }
2ronwalf else if(atom instanceof DifferentIndividualsAtom) {
2ronwalf DifferentIndividualsAtom diffAtom = (DifferentIndividualsAtom) atom;
2ronwalf newAtom = swrl.createDifferentIndividualsAtom(
2ronwalf getIndidividualObject( diffAtom.getArgument1(), binding ),
2ronwalf getIndidividualObject( diffAtom.getArgument2(), binding ));
2ronwalf }
2ronwalf else if(atom instanceof BuiltinAtom) {
2ronwalf BuiltinAtom builtinAtom = (BuiltinAtom) atom;
2ronwalf OWLIndividual builtin = builtinAtom.getBuiltin();
2ronwalf int argCount = atom.getArgumentCount();
2ronwalf SWRLDataObject[] args = new SWRLDataObject[ argCount ];
2ronwalf for( int i = 0; i < argCount; i++ ) {
2ronwalf SWRLDataObject arg = (SWRLDataObject) atom.getArgument( i );
2ronwalf args[i] = getDataObject( arg, binding );
2ronwalf }
2ronwalf
2ronwalf if ( builtin.equals( SWRLB.equal ) )
2ronwalf newAtom = swrl.createEqual( args[0], args[1] );
2ronwalf else if ( builtin.equals( SWRLB.notEqual ) )
2ronwalf newAtom = swrl.createNotEqual( args[0], args[1] );
2ronwalf else if ( builtin.equals( SWRLB.greaterThan ) )
2ronwalf newAtom = swrl.createGreaterThan( args[0], args[1] );
2ronwalf else if ( builtin.equals( SWRLB.greaterThanOrEqual ) )
2ronwalf newAtom = swrl.createGreaterThanOrEqual( args[0], args[1] );
2ronwalf else if ( builtin.equals( SWRLB.lessThan ) )
2ronwalf newAtom = swrl.createLessThan( args[0], args[1] );
2ronwalf else if ( builtin.equals( SWRLB.lessThanOrEqual ) )
2ronwalf newAtom = swrl.createLessThanOrEqual( args[0], args[1] );
2ronwalf else if ( builtin.equals( SWRLB.add ) )
2ronwalf newAtom = swrl.createAdd( args[0], args[1], args[2] );
2ronwalf else if ( builtin.equals( SWRLB.subtract ) )
2ronwalf newAtom = swrl.createSubtract( args[0], args[1], args[2] );
2ronwalf else
2ronwalf throw new NotImplementedException( builtin.toPrettyString() );
2ronwalf }
2ronwalf else
2ronwalf throw new RuntimeException("Unknown atom type " + atom);
2ronwalf
2ronwalf if( newList.isEmpty() )
2ronwalf newList = newList.insert( newAtom );
2ronwalf else
2ronwalf newList.add( newAtom );
2ronwalf
2ronwalf// newList = newList.add( newAtom );
2ronwalf }
2ronwalf
2ronwalf return newList;
2ronwalf }
2ronwalf
2ronwalf
2ronwalf public List getVars() {
2ronwalf List vars = new ArrayList();
2ronwalf
2ronwalf for( AtomList list = this; !list.isEmpty(); list = (AtomList) list.getRest() ) {
2ronwalf Atom atom = (Atom) list.getFirst();
2ronwalf for( int j = 0; j < atom.getArgumentCount(); j++ ) {
2ronwalf SWRLObject term = atom.getArgument( j );
2ronwalf if( term instanceof Variable )
2ronwalf vars.add( term );
2ronwalf }
2ronwalf }
2ronwalf return vars;
2ronwalf }
2ronwalf
2ronwalf public String toString() {
2ronwalf StringBuffer str = new StringBuffer("[");
2ronwalf for(RDFList list = this; !list.isEmpty(); list = list.getRest()) {
2ronwalf if(list != this)
2ronwalf str.append(", ");
2ronwalf str.append(list.getFirst().toString());
2ronwalf }
2ronwalf str.append("]");
2ronwalf return str.toString();
2ronwalf }
16daenzerorama
16daenzerorama public RDFList remove() {
18daenzerorama AtomList list = (AtomList) getRest();
18daenzerorama
16daenzerorama if (size() > 1) {
16daenzerorama list.setFirst(getRest().getFirstValue());
18daenzerorama list.setRest((AtomList) getRest().getRest());
18daenzerorama } else {
18daenzerorama list = (AtomList) vocabulary.nil().castTo(RDFList.class);
16daenzerorama }
18daenzerorama if (hasProperty(vocabulary.first()))
18daenzerorama removeProperties(vocabulary.first());
18daenzerorama if (hasProperty(vocabulary.rest()))
18daenzerorama removeProperties(vocabulary.rest());
18daenzerorama individual.delete();
18daenzerorama
16daenzerorama return list;
16daenzerorama }
18daenzerorama
18daenzerorama public RDFList removeAll() {
18daenzerorama AtomList list = this;
18daenzerorama while (list.size() > 0)
18daenzerorama list = (AtomList) list.remove();
18daenzerorama return list;
18daenzerorama }
2ronwalf}