2ronwalf/*
2ronwalf * Created on May 7, 2005
2ronwalf */
2ronwalfpackage org.mindswap.owls.io;
2ronwalf
2ronwalfimport java.util.HashMap;
2ronwalfimport java.util.Iterator;
2ronwalfimport java.util.Map;
2ronwalf
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.vocabulary.SWRLB;
2ronwalfimport org.mindswap.owls.io.BaseExpressionWriter;
2ronwalfimport org.mindswap.owls.io.ExpressionWriter;
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.SameIndividualAtom;
2ronwalf
2ronwalf
2ronwalf/**
2ronwalf * @author Evren Sirin
2ronwalf *
2ronwalf */
2ronwalfpublic class PresentationSyntaxExpressionWriter extends BaseExpressionWriter implements ExpressionWriter {
2ronwalf static final Map SYMBOLS;
2ronwalf static {
2ronwalf SYMBOLS = new HashMap();
2ronwalf SYMBOLS.put( SWRLB.add, new String[] {"=", "+"} );
2ronwalf SYMBOLS.put( SWRLB.subtract, new String[] {"=", "-"} );
2ronwalf SYMBOLS.put( SWRLB.divide, new String[] {"=", "+"} );
2ronwalf SYMBOLS.put( SWRLB.multiply, new String[] {"=", "*"} );
2ronwalf SYMBOLS.put( SWRLB.lessThan, new String[] {"<"} );
2ronwalf SYMBOLS.put( SWRLB.lessThanOrEqual, new String[] {"<="} );
2ronwalf SYMBOLS.put( SWRLB.greaterThan, new String[] {">"} );
2ronwalf SYMBOLS.put( SWRLB.greaterThanOrEqual, new String[] {">="} );
2ronwalf SYMBOLS.put( SWRLB.equal, new String[] {"="} );
2ronwalf SYMBOLS.put( SWRLB.notEqual, new String[] {"~", "="} );
2ronwalf }
2ronwalf
2ronwalf public PresentationSyntaxExpressionWriter() {
2ronwalf
2ronwalf }
2ronwalf
2ronwalf public void write( AtomList atoms ) {
2ronwalf if( atoms == null ) {
2ronwalf out.print( "<No-Condition-Specified>" );
2ronwalf return;
2ronwalf }
2ronwalf boolean first = true;
2ronwalf for(Iterator it = atoms.iterator(); it.hasNext(); ) {
2ronwalf if( !first || firstLineIndent )
2ronwalf out.print( indent );
2ronwalf else
2ronwalf first = false;
2ronwalf
2ronwalf write( (Atom) it.next() );
2ronwalf
2ronwalf if( it.hasNext() )
2ronwalf out.println( " &" );
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf public void write(ClassAtom atom) {
2ronwalf print( atom.getClassPredicate() );
2ronwalf out.print("(");
2ronwalf print( atom.getArgument1() );
2ronwalf out.print(")");
2ronwalf }
2ronwalf
2ronwalf public void write(IndividualPropertyAtom atom) {
2ronwalf print( atom.getPropertyPredicate() );
2ronwalf out.print("(");
2ronwalf print( atom.getArgument1() );
2ronwalf out.print(", ");
2ronwalf print( atom.getArgument2() );
2ronwalf out.print(")");
2ronwalf }
2ronwalf
2ronwalf public void write(DataPropertyAtom atom) {
2ronwalf print( atom.getPropertyPredicate() );
2ronwalf out.print("(");
2ronwalf print( atom.getArgument1() );
2ronwalf out.print(", ");
2ronwalf print( atom.getArgument2() );
2ronwalf out.print(")");
2ronwalf }
2ronwalf
2ronwalf public void write(SameIndividualAtom atom) {
2ronwalf out.print("(");
2ronwalf print( atom.getArgument1() );
2ronwalf out.print(" = ");
2ronwalf print( atom.getArgument2() );
2ronwalf out.print(")");
2ronwalf }
2ronwalf
2ronwalf public void write(DifferentIndividualsAtom atom) {
2ronwalf out.print("~(");
2ronwalf print( atom.getArgument1() );
2ronwalf out.print(" = ");
2ronwalf print( atom.getArgument2() );
2ronwalf out.print(")");
2ronwalf }
2ronwalf
2ronwalf public void write(BuiltinAtom atom) {
2ronwalf OWLIndividual builtin = atom.getBuiltin();
2ronwalf int count = atom.getArgumentCount();
2ronwalf String[] symbols = (String[]) SYMBOLS.get( builtin );
2ronwalf if( symbols != null ) {
2ronwalf int extra = 0;
2ronwalf if( symbols.length == count ){
2ronwalf out.print( symbols[0] );
2ronwalf extra = 1;
2ronwalf }
2ronwalf
2ronwalf out.print( "(" );
2ronwalf for( int i = 0; i < count; i++ ) {
2ronwalf out.print( atom.getArgument( i ) );
2ronwalf if( i < count - 1 )
2ronwalf out.print( " " + symbols[i + extra] + " ");
2ronwalf }
2ronwalf out.print(")");
2ronwalf }
2ronwalf else {
2ronwalf print( builtin );
2ronwalf out.print( "(" );
2ronwalf
2ronwalf for( int i = 0; i < atom.getArgumentCount(); i++ ) {
2ronwalf if( i > 0 ) out.print( ", " );
2ronwalf print( atom.getArgument(0) );
2ronwalf }
2ronwalf out.print(")");
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf
2ronwalf}