package gve.calc.formula;

import java.awt.*;
import gve.calc.formula.*;

public class RewriteRule extends Tabular {
	Part [] rules;	// naam regel, voorwaarden, conclusie

	public RewriteRule() {
		rules = new Part[1];
		rules[0] = new Identifier("");
		rules[0].setParent(this);
	}

	public String getName() {
		Part p = rules[0];
		if (p instanceof OperatorSpace)	// rule with parameters
			p = ((OperatorSpace)p).left;
		if (p instanceof Identifier)
			return ((Identifier)p).getString();
		return null;
	}

	
How many parameters has this rule? E.g. if the name is "G" then 0 params; "G x" has 1 param; "S t x" has 2 etc
public int getParamCount() { int result = 0; Part p = rules[0]; // Number of parameters is number of OperatorSpaces while (p instanceof OperatorSpace) { p = ((OperatorSpace)p).right; result++; } return result; } public Part getConclusion() { return rules[rules.length-1]; } public Object clone() { RewriteRule result = new RewriteRule(); result.rules = new Part[rules.length]; for (int i = rules.length-1; i>=0; i--) { result.rules[i] = (Part)rules[i].clone(); result.rules[i].setParent(result); } return result; } public int getHeight() { return rules.length; } public int getWidth() { return 1; } public Part elementAt(int i,int j) { return rules[i]; } public void setElementAt(int i,int j,Part el) { rules[i] = el; el.setParent(this); } public void removeRow(int pos) { rules = removeRow(rules,pos); super.removeRow(pos); } public void insertRow(int pos) { rules = insertRow(rules,pos); super.insertRow(pos); } public void insertCol(int pos) {} public void write(java.io.Writer w) throws java.io.IOException { super.write(w); w.write(""+getHeight()); w.write("\n"); writeTable(w); } public static Part read(java.io.BufferedReader r) throws java.io.IOException, ClassNotFoundException,NoSuchMethodException, java.lang.reflect.InvocationTargetException,IllegalAccessException { int height = Integer.parseInt(r.readLine()); RewriteRule result = new RewriteRule(); result.rules = new Part[height]; result.readTable(r); return result; } public Component createView(FormulaView view) { return new RewriteRuleView(this,view); } }