crowlogic / arb4j

arb4j is a Java API for the arbitrary precision ball arithmetic library found at http://arblib.org
Other
1 stars 0 forks source link

analayzer: implement groupings of expressions which are really systems of equations that share a common Context #23 #508

Open crowlogic opened 1 month ago

crowlogic commented 1 month ago

Here's how we can modify the ExpressionTree class to include the EquationTypeSelector:

  1. First, let's update the ExpressionTree class:
public class ExpressionTree<D, C, F extends Function<D, C>> extends VBox {
    // ... existing fields ...
    private EquationTypeSelector equationTypeSelector;

    public ExpressionTree(Analyzer<D, C, F> expressionAnalyzer) {
        super(10);
        this.analyzer = expressionAnalyzer;
        this.context = new Context(Integer.named("input").set(3),
                                   Real.named("v").set(RealConstants.half));

        setupEquationTypeSelector();
        setupExpressionInput();

        MiniSymbolPalette symbolPalette = new MiniSymbolPalette(expressionInput);

        HBox inputRow = new HBox(10);
        inputRow.getChildren().addAll(equationTypeSelector, expressionInput, symbolPalette);
        HBox.setHgrow(expressionInput, Priority.ALWAYS);

        setupTreeTableView();

        stackPane = new StackPane(treeTableView);
        getChildren().addAll(inputRow, stackPane);
        setPadding(new Insets(10));

        VBox.setVgrow(stackPane, Priority.ALWAYS);
        HBox.setHgrow(stackPane, Priority.ALWAYS);
        VBox.setVgrow(this, Priority.ALWAYS);
    }

    private void setupEquationTypeSelector() {
        equationTypeSelector = new EquationTypeSelector();
        equationTypeSelector.setDomainType(Real.class);  // Set default values
        equationTypeSelector.setCodomainType(Real.class);
        equationTypeSelector.setFunctionType(RealFunction.class);
    }

    // ... existing methods ...

    @SuppressWarnings("unchecked")
    public void compileExpression() {
        String expressionString = expressionInput.getText();
        try {
            Class<D> domainType = (Class<D>) equationTypeSelector.getDomainType();
            Class<C> codomainType = (Class<C>) equationTypeSelector.getCodomainType();
            Class<F> functionType = (Class<F>) equationTypeSelector.getFunctionType();

            expr = Function.compile(domainType, codomainType, functionType, expressionString, context.resetClassLoader());

            instance = expr.instantiate();
            updateTreeTableView();
            updateContextView();
        } catch (Throwable e) {
            e.printStackTrace(System.err);
            this.analyzer.showAlert("Compilation Error", e.getClass().getName(), e.getMessage());
        }
    }

    // ... rest of the class ...
}
  1. Now, let's update the Analyzer class to remove any references to the type selection boxes:
public class Analyzer<D, C, F extends Function<D, C>> extends Application {
    // Remove these fields if they exist
    // public final ComboBox<Class<?>> codomainTypeBox;
    // public final ComboBox<Class<?>> domainTypeBox;
    // public final ComboBox<Class<?>> functionTypeBox;

    // ... rest of the class ...

    public VBox createMainLayout() {
        tabPane = new TabPane();

        var mainLayout = new VBox(10);

        var buttonBox = new HBox(10, newButtonBox());

        var scrollpane = createScrollPane();

        mainLayout.getChildren().addAll(buttonBox, scrollpane);

        return mainLayout;
    }

    // Remove the setupTypeBoxes method if it exists

    // ... rest of the class ...
}
  1. Ensure that the EquationTypeSelector class is properly implemented:
public class EquationTypeSelector extends Control {
    private final ObjectProperty<Class<?>> domainTypeProperty = new SimpleObjectProperty<>(this, "domainType", Real.class);
    private final ObjectProperty<Class<?>> codomainTypeProperty = new SimpleObjectProperty<>(this, "codomainType", Real.class);
    private final ObjectProperty<Class<?>> functionTypeProperty = new SimpleObjectProperty<>(this, "functionType", RealFunction.class);

    private ComboBox<Class<?>> domainTypeBox;
    private ComboBox<Class<?>> codomainTypeBox;
    private ComboBox<Class<?>> functionTypeBox;

    public EquationTypeSelector() {
        getStyleClass().add("equation-type-selector");
        createContent();
    }

    private void createContent() {
        domainTypeBox = new ComboBox<>();
        codomainTypeBox = new ComboBox<>();
        functionTypeBox = new ComboBox<>();

        setupTypeBoxes();

        HBox layout = new HBox(10,
                               new Label("Domain:"), domainTypeBox,
                               new Label("Codomain:"), codomainTypeBox,
                               new Label("Function Type:"), functionTypeBox);

        getChildren().add(layout);
    }

    private void setupTypeBoxes() {
        domainTypeBox.getItems().addAll(Real.class, Complex.class, Integer.class /* ... */);
        codomainTypeBox.getItems().addAll(Real.class, Complex.class, Integer.class /* ... */);
        functionTypeBox.getItems().addAll(RealFunction.class, ComplexFunction.class /* ... */);

        domainTypeBox.valueProperty().bindBidirectional(domainTypeProperty);
        codomainTypeBox.valueProperty().bindBidirectional(codomainTypeProperty);
        functionTypeBox.valueProperty().bindBidirectional(functionTypeProperty);
    }

    // Getter and setter methods for domainType, codomainType, and functionType
    // ...
}

These changes will:

  1. Add an EquationTypeSelector to each ExpressionTree instance.
  2. Allow each expression (tab) to have its own type settings.
  3. Remove any global type selection from the Analyzer class.
  4. Make the design more modular and encapsulated, with each expression managing its own types.

Remember to update any other parts of your code that might have been referencing the type boxes in the Analyzer class. Each ExpressionTree now manages its own types through its EquationTypeSelector.