Search in sources :

Example 1 with FormulaInput

use of org.csstudio.trends.databrowser3.model.FormulaInput in project org.csstudio.display.builder by kasemir.

the class EditFormulaDialogDemo method dialogDemo.

public void dialogDemo() throws Exception {
    final Shell shell = new Shell();
    // Demo model with some PVs and a formula
    final Model model = new Model();
    model.addItem(new PVItem("fred", 0.0));
    model.addItem(new PVItem("freddy", 0.0));
    model.addItem(new PVItem("jane", 0.0));
    model.addItem(new PVItem("janet", 0.0));
    final FormulaItem formula = new FormulaItem("demo", "2*x2", new FormulaInput[] { new FormulaInput(model.getItem("fred"), "x2"), new FormulaInput(model.getItem("janet"), "jj") });
    model.addItem(formula);
    final EditFormulaDialog edit = new EditFormulaDialog(null, shell, formula);
    System.out.println("Before editing:");
    dump(formula);
    if (edit.open()) {
        System.out.println("After editing:");
        dump(formula);
    } else
        System.out.println("Cancelled");
}
Also used : FormulaItem(org.csstudio.trends.databrowser3.model.FormulaItem) Shell(org.eclipse.swt.widgets.Shell) Model(org.csstudio.trends.databrowser3.model.Model) FormulaInput(org.csstudio.trends.databrowser3.model.FormulaInput) PVItem(org.csstudio.trends.databrowser3.model.PVItem)

Example 2 with FormulaInput

use of org.csstudio.trends.databrowser3.model.FormulaInput in project org.csstudio.display.builder by kasemir.

the class AddModelItemCommand method forFormula.

/**
 * Create PV via undo-able AddModelItemCommand,
 *  displaying errors in dialog
 *  @param shell Shell used for error dialogs
 *  @param operations_manager OperationsManager where command will be reg'ed
 *  @param model Model were PV is to be added
 *  @param axis Axis
 *  @return AddModelItemCommand or <code>null</code> on error
 */
public static Optional<AddModelItemCommand> forFormula(final Shell shell, final UndoableActionManager operations_manager, final Model model, final String formula_name, final AxisConfig axis) {
    // Create item
    final FormulaItem item;
    try {
        // $NON-NLS-1$
        item = new FormulaItem(formula_name, "0", new FormulaInput[0]);
        axis.setVisible(true);
        item.setAxis(axis);
    } catch (Exception ex) {
        MessageDialog.openError(shell, Messages.Error, NLS.bind(Messages.AddItemErrorFmt, formula_name, ex.getMessage()));
        return Optional.empty();
    }
    // Add to model via undo-able command
    return Optional.of(new AddModelItemCommand(shell, operations_manager, model, item));
}
Also used : FormulaItem(org.csstudio.trends.databrowser3.model.FormulaItem) FormulaInput(org.csstudio.trends.databrowser3.model.FormulaInput)

Example 3 with FormulaInput

use of org.csstudio.trends.databrowser3.model.FormulaInput in project org.csstudio.display.builder by kasemir.

the class EditFormulaDialog method determineInputs.

/**
 * @return List of inputs for formula: Each model item is a possible input,
 *          mapped to a variable name that's either already used in the
 *          formula for that model item, or a simple "x1", "x2", ... when
 *          not already used
 */
@SuppressWarnings("nls")
private InputItem[] determineInputs() {
    final Model model = formula.getModel().get();
    // Create list of inputs.
    final ArrayList<InputItem> inputs = new ArrayList<InputItem>();
    // Every model item is a possible input.
    model_loop: for (ModelItem model_item : model.getItems()) {
        // Formula cannot be an input to itself
        if (model_item == formula)
            continue;
        // Create InputItem for that ModelItem
        InputItem input = null;
        // See if model item is already used in the formula
        for (FormulaInput existing_input : formula.getInputs()) {
            if (existing_input.getItem() == model_item) {
                // Yes, use the existing variable name
                input = new InputItem(model_item.getName(), existing_input.getVariableName());
                break;
            }
        }
        // If input is unused, assign variable name x1, x2, ...
        if (input == null) {
            for (InputItem existing_item : inputs) if (existing_item.getInputName().equals(model_item.getName())) {
                // The item with the same name was already added to the input list.
                continue model_loop;
            }
            // Try "x1", then "xx1", "xxx1" until an unused name is found
            String var_name = Integer.toString(inputs.size() + 1);
            boolean name_in_use;
            do {
                name_in_use = false;
                var_name = "x" + var_name;
                for (final FormulaInput existing_input : formula.getInputs()) if (existing_input.getVariableName().equals(var_name)) {
                    name_in_use = true;
                    break;
                }
            } while (name_in_use);
            input = new InputItem(model_item.getName(), var_name);
        }
        inputs.add(input);
    }
    return inputs.toArray(new InputItem[inputs.size()]);
}
Also used : InputItem(org.csstudio.apputil.ui.formula.InputItem) Model(org.csstudio.trends.databrowser3.model.Model) ArrayList(java.util.ArrayList) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) FormulaInput(org.csstudio.trends.databrowser3.model.FormulaInput)

Example 4 with FormulaInput

use of org.csstudio.trends.databrowser3.model.FormulaInput in project org.csstudio.display.builder by kasemir.

the class EditFormulaDialog method open.

/**
 * Open, i.e. display the dialog.
 *  @return <code>true</code> when the item was updated,
 *          <code>false</code> for 'cancel'
 */
public boolean open() {
    try {
        // Edit
        dialog = new FormulaDialog(shell, formula.getExpression(), determineInputs());
        if (dialog.open() != Window.OK)
            return false;
        // Update model item with new formula from dialog
        final Model model = formula.getModel().get();
        final ArrayList<FormulaInput> new_inputs = new ArrayList<FormulaInput>();
        for (final InputItem input : dialog.getInputs()) {
            final ModelItem item = model.getItem(input.getInputName());
            if (item == null)
                // $NON-NLS-1$
                throw new Exception("Cannot locate formula input " + input.getInputName());
            new_inputs.add(new FormulaInput(item, input.getVariableName()));
        }
        // Update formula via undo-able command
        new ChangeFormulaCommand(shell, operations_manager, formula, dialog.getFormula(), new_inputs.toArray(new FormulaInput[new_inputs.size()]));
    } catch (final Exception ex) {
        ExceptionDetailsErrorDialog.openError(shell, Messages.Error, ex);
        return false;
    }
    return true;
}
Also used : FormulaDialog(org.csstudio.apputil.ui.formula.FormulaDialog) InputItem(org.csstudio.apputil.ui.formula.InputItem) Model(org.csstudio.trends.databrowser3.model.Model) ArrayList(java.util.ArrayList) FormulaInput(org.csstudio.trends.databrowser3.model.FormulaInput) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem)

Example 5 with FormulaInput

use of org.csstudio.trends.databrowser3.model.FormulaInput in project org.csstudio.display.builder by kasemir.

the class ControllerDemo method createModel.

private void createModel() throws Exception {
    model = new Model();
    model.setMacros(new TestMacros());
    ModelItem item;
    item = new PVItem("$(simu)", 1);
    item.setDisplayName("$(name)");
    item.setAxis(model.addAxis());
    model.addItem(item);
    item = new FormulaItem("math", "sine*0.5+2", new FormulaInput[] { new FormulaInput(item, "sine") });
    item = new PVItem("sim://ramp", 0);
    item.setDisplayName("Ramp (monitored)");
    item.setAxis(model.addAxis());
    model.addItem(item);
    final ArchiveDataSource archive = new ArchiveDataSource("jdbc:oracle:thin:sns_reports/sns@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=OFF)(ADDRESS=(PROTOCOL=TCP)(HOST=172.31.75.138)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=172.31.75.141)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ics_prod_lba)))", 1, "rdb");
    item = new PVItem("CCL_LLRF:IOC1:Load", 0);
    ((PVItem) item).addArchiveDataSource(archive);
    item.setDisplayName("CCL 1 CPU Load (monitored)");
    item.setAxis(model.addAxis());
    model.addItem(item);
    item = new PVItem("DTL_LLRF:IOC1:Load", 1.0);
    ((PVItem) item).addArchiveDataSource(archive);
    item.setDisplayName("DTL 1 CPU Load (1 sec)");
    item.setAxis(model.addAxis());
    model.addItem(item);
    item = new FormulaItem("calc", "dtl-10", new FormulaInput[] { new FormulaInput(item, "dtl") });
    item.setDisplayName("Lessened Load");
    item.setAxis(model.getAxis(2));
    model.addItem(item);
}
Also used : FormulaItem(org.csstudio.trends.databrowser3.model.FormulaItem) Model(org.csstudio.trends.databrowser3.model.Model) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) FormulaInput(org.csstudio.trends.databrowser3.model.FormulaInput) ArchiveDataSource(org.csstudio.trends.databrowser3.model.ArchiveDataSource) PVItem(org.csstudio.trends.databrowser3.model.PVItem)

Aggregations

FormulaInput (org.csstudio.trends.databrowser3.model.FormulaInput)5 Model (org.csstudio.trends.databrowser3.model.Model)4 FormulaItem (org.csstudio.trends.databrowser3.model.FormulaItem)3 ModelItem (org.csstudio.trends.databrowser3.model.ModelItem)3 ArrayList (java.util.ArrayList)2 InputItem (org.csstudio.apputil.ui.formula.InputItem)2 PVItem (org.csstudio.trends.databrowser3.model.PVItem)2 FormulaDialog (org.csstudio.apputil.ui.formula.FormulaDialog)1 ArchiveDataSource (org.csstudio.trends.databrowser3.model.ArchiveDataSource)1 Shell (org.eclipse.swt.widgets.Shell)1