1 8 package org.lsmp.djep.xjep; 9 10 import org.nfunk.jep.function.PostfixMathCommandI; 11 import org.nfunk.jep.*; 12 13 34 public class XOperator extends Operator { 35 36 public static final int NO_ARGS=0; 37 38 public static final int UNARY=1; 39 40 public static final int BINARY=2; 41 42 public static final int NARY=3; 43 44 public static final int LEFT=4; 45 46 public static final int RIGHT=8; 47 48 public static final int ASSOCIATIVE=16; 49 50 public static final int COMMUTATIVE=32; 51 52 public static final int REFLEXIVE=64; 53 54 public static final int SYMMETRIC=128; 55 56 public static final int TRANSITIVE=256; 57 58 public static final int EQUIVILENCE=TRANSITIVE+REFLEXIVE+SYMMETRIC; 59 60 public static final int PREFIX=512; 61 62 public static final int SUFIX=1024; 63 64 public static final int SELF_INVERSE=2048; 65 66 public static final int COMPOSITE=4096; 67 71 public static final int USE_BINDING_FOR_PRINT=8192; 72 73 private int flags; 74 75 81 public XOperator(String name,PostfixMathCommandI pfmc,int flags) 82 { 83 super(name,pfmc); 84 this.flags = flags; 85 } 86 93 public XOperator(String name,PostfixMathCommandI pfmc,int flags,int precedence) 94 { 95 this(name,pfmc,flags); 96 this.precedence=precedence; 97 } 98 105 public XOperator(String name,String symbol,PostfixMathCommandI pfmc,int flags) 106 { 107 super(name,symbol,pfmc); 108 this.flags = flags; 109 } 110 117 public XOperator(String name,String symbol,PostfixMathCommandI pfmc,int flags,int precedence) 118 { 119 super(name,symbol,pfmc); 120 this.precedence=precedence; 121 this.flags=flags; 122 } 123 124 public XOperator(Operator op,int flags,int precedence) 125 { 126 this(op.getName(),op.getSymbol(),op.getPFMC(),flags,precedence); 127 } 128 129 public XOperator(Operator op,int flags) 130 { 131 this(op.getName(),op.getSymbol(),op.getPFMC(),flags); 132 } 133 134 139 140 private int precedence = -1; 141 public final int getPrecedence() {return precedence;} 142 protected final void setPrecedence(int i) {precedence = i;} 143 144 145 private Operator distribOver[] = new Operator[0]; 146 147 protected final void setDistributiveOver(Operator op) 148 { 149 int len = distribOver.length; 150 Operator temp[] = new Operator[len+1]; 151 for(int i=0;i<len;++i) temp[i] = distribOver[i]; 152 temp[len]=op; 153 distribOver=temp; 154 } 155 public boolean isDistributiveOver(Operator op) 156 { 157 for(int i=0;i<distribOver.length;++i) 158 if(op == distribOver[i]) 159 return true; 160 return false; 161 } 162 163 164 private Operator rootOperator=null; 165 private Operator inverseOperator=null; 166 private Operator binaryInverseOperator=null; 167 protected void setRootOp(Operator root) {rootOperator=root;} 168 protected void setInverseOp(Operator inv){inverseOperator = inv;} 169 protected void setBinaryInverseOp(Operator inv){binaryInverseOperator = inv;} 170 public Operator getRootOp() { return rootOperator; } 171 public Operator getInverseOp() { return inverseOperator; } 172 public Operator getBinaryInverseOp() { return binaryInverseOperator; } 173 174 179 public final int getBinding() { return (flags & (LEFT | RIGHT)); } 180 public final boolean isAssociative() {return ((flags & ASSOCIATIVE) == ASSOCIATIVE);} 181 public final boolean isCommutative() { return ((flags & COMMUTATIVE) == COMMUTATIVE);} 182 public final boolean isBinary() { return ((flags & 3) == BINARY); } 183 public final boolean isUnary() { return ((flags & 3) == UNARY); } 184 public final boolean isNary() { return ((flags & 3) == NARY); } 185 public final int numArgs() { return (flags & 3); } 186 public final boolean isTransitive() { return ((flags & TRANSITIVE) == TRANSITIVE); } 187 public final boolean isSymmetric() { return ((flags & SYMMETRIC) == SYMMETRIC); } 188 public final boolean isReflexive() { return ((flags & REFLEXIVE) == REFLEXIVE); } 189 public final boolean isEquivilence() {return ((flags & EQUIVILENCE) == EQUIVILENCE); } 190 public final boolean isPrefix() {return ((flags & PREFIX) == PREFIX); } 191 public final boolean isSufix() {return ((flags & SUFIX) == SUFIX); } 192 public final boolean isComposite() {return ((flags & COMPOSITE) == COMPOSITE); } 193 public final boolean isSelfInverse() {return ((flags & SELF_INVERSE) == SELF_INVERSE); } 194 public final boolean useBindingForPrint() {return ((flags & USE_BINDING_FOR_PRINT) == USE_BINDING_FOR_PRINT); } 195 196 197 198 public String toFullString() 199 { 200 StringBuffer sb = new StringBuffer (); 201 sb.append("Operator: \""+getSymbol()+"\""); 202 if(!getName().equals(getSymbol())) sb.append(" "+getName()); 203 switch(numArgs()){ 204 case 0: sb.append(" no arguments,"); break; 205 case 1: sb.append(" unary,"); break; 206 case 2: sb.append(" binary,"); break; 207 case 3: sb.append(" variable number of arguments,"); break; 208 } 209 if(isPrefix() && isSufix()) sb.append(" trifix,"); 210 else if(isPrefix()) sb.append(" prefix,"); 211 else if(isSufix()) sb.append(" sufix,"); 212 else sb.append(" infix,"); 213 if(getBinding()==LEFT) sb.append(" left binding,"); 214 else if(getBinding()==RIGHT) sb.append(" right binding,"); 215 if(isAssociative()) sb.append(" associative,"); 216 if(isCommutative()) sb.append(" commutative,"); 217 sb.append(" precedence "+getPrecedence()+","); 218 if(isEquivilence()) 219 sb.append(" equivilence relation,"); 220 else 221 { 222 if(isReflexive()) sb.append(" reflexive,"); 223 if(isSymmetric()) sb.append(" symmetric,"); 224 if(isTransitive()) sb.append(" transitive,"); 225 } 226 sb.setCharAt(sb.length()-1,'.'); 227 return sb.toString(); 228 } 229 } 230 | Popular Tags |