public code v1

This commit is contained in:
2026-05-22 11:14:29 +02:00
parent 427197ec5a
commit b8141736eb
28859 changed files with 575079 additions and 0 deletions
@@ -0,0 +1,13 @@
package afryca.domain.gui.fuzzy.l10n;
public class Messages {
public String at_least_3_labels_are_required;
public String create_fuzzy_set;
public String domain_name;
public String duplicate_name;
public String duplicate_label;
public String empty_domain;
public String empty_value;
public String insert_labels_with_separator_colon;
public String labels;
}
@@ -0,0 +1,9 @@
at_least_3_labels_are_required=At least 3 labels are required
create_fuzzy_set=Create fuzzy set
domain_name=Domain name
duplicate_name=Duplicated name
duplicate_label=Duplicate label
empty_domain=Empty domain
empty_value=Empty value
insert_labels_with_separator_colon=Insert labels with separator colon ':'
labels=Labels
@@ -0,0 +1,9 @@
at_least_3_labels_are_required=Al menos 3 etiquetas son requeridas
create_fuzzy_set=Crear dominio lingüístico difuso
domain_name=Nombre dominio
duplicate_name=Nombre duplicado
duplicate_label=Etiqueta duplicada
empty_domain=Dominio vacío
empty_value=Valor vacío
insert_labels_with_separator_colon=Insertar etiquetas separadas por dos puntos ':'
labels=Etiquetas
@@ -0,0 +1,349 @@
package afryca.domain.gui.fuzzyset.chart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.swt.widgets.Composite;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.experimental.chart.swt.ChartComposite;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleInsets;
import afryca.domain.Domain;
import afryca.domain.fuzzyset.FuzzySet;
import afryca.domain.fuzzyset.function.types.TrapezoidalFunction;
import afryca.domain.fuzzyset.labelset.Label;
import afryca.domain.fuzzyset.semantic.IMembershipFunction;
import afryca.domain.gui.chart.DomainChart;
public class FuzzyChart extends DomainChart {
private XYSeriesCollection dataset;
public static final Color[] colors = { Color.RED, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.LIGHT_GRAY,
Color.ORANGE, Color.ORANGE, Color.MAGENTA, Color.PINK, Color.WHITE, Color.YELLOW };
private ValueMarker[] _alternativesMarkers;
public FuzzyChart() {
domain = null;
chart = null;
chartComposite = null;
}
@Override
public void initialize(Domain domain, Composite container, int width, int height, int style) {
setDomain(domain);
chartComposite = new ChartComposite(container, style, chart, true);
chartComposite.setSize(width, height);
}
@Override
public void refreshChart() {
if (chart == null) {
chart = createChart(createDataset());
} else {
chart.getXYPlot().setDataset(createDataset());
}
setBasicRenderer(chart.getXYPlot());
}
@SuppressWarnings("unchecked")
@Override
public void setSelection(Object selection) {
XYPlot xyplot = (XYPlot) chart.getPlot();
XYItemRenderer renderer = xyplot.getRenderer(0);
if (!(selection instanceof LinkedList<?>)) {
for (int i = 0; i < xyplot.getSeriesCount(); ++i) {
if (i != (Integer) selection) {
renderer.setSeriesStroke(i, new BasicStroke(1));
} else {
renderer.setSeriesStroke(i, new BasicStroke(3));
}
}
} else {
if (((LinkedList<Integer>) selection).size() > 1) {
int lower = ((LinkedList<Integer>) selection).get(0);
int upper = ((LinkedList<Integer>) selection).get(1);
for (int i = 0; i < xyplot.getSeriesCount(); ++i) {
if ((lower <= i) && (i <= upper)) {
renderer.setSeriesStroke(i, new BasicStroke(3));
} else {
renderer.setSeriesStroke(i, new BasicStroke(1));
}
}
} else {
int value = ((LinkedList<Integer>) selection).get(0);
for (int i = 0; i < xyplot.getSeriesCount(); ++i) {
if (i != value) {
renderer.setSeriesStroke(i, new BasicStroke(1));
} else {
renderer.setSeriesStroke(i, new BasicStroke(3));
}
}
}
}
}
private JFreeChart createChart(XYSeriesCollection dataset) {
JFreeChart result = ChartFactory.createXYLineChart("", "", "", dataset, PlotOrientation.VERTICAL, true, true, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
false);
result.setBackgroundPaint(Color.WHITE);
XYPlot xyplot = (XYPlot) result.getPlot();
xyplot.setBackgroundPaint(Color.WHITE);
xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY);
xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY);
xyplot.getDomainAxis().setRange(0d, 1d);
xyplot.getRangeAxis().setRange(0d, 1.1d);
setBasicRenderer(xyplot);
return result;
}
private XYSeriesCollection createDataset() {
dataset = new XYSeriesCollection();
if (domain != null) {
if (((FuzzySet) domain).getLabelSet().getCardinality() > 0) {
XYSeries series;
for (Label label : ((FuzzySet) domain).getLabelSet().getLabels()) {
series = new XYSeries(label.getName());
IMembershipFunction membershipFunction = label.getSemantic();
if (membershipFunction instanceof TrapezoidalFunction) {
TrapezoidalFunction trapezoidalFunction = (TrapezoidalFunction) membershipFunction;
series.add(trapezoidalFunction.getCoverage().getMin(), 0.0);
series.add(trapezoidalFunction.getCenter().getMin(), 1.0);
series.add(trapezoidalFunction.getCenter().getMax(), 1.0);
series.add(trapezoidalFunction.getCoverage().getMax(), 0.0);
}
dataset.addSeries(series);
}
}
}
return dataset;
}
private Color colorForEachLabel(int pos) {
int r, g, b;
r = (63 * (pos + 1)) % 255;
g = (107 * (pos + 2)) % 255;
b = (217 * (pos + 3)) % 255;
return new Color(r, g, b);
}
private void setBasicRenderer(XYPlot xyplot) {
XYItemRenderer renderer = xyplot.getRenderer(0);
for (int i = 0; i < xyplot.getSeriesCount(); ++i) {
renderer.setSeriesStroke(i, new BasicStroke());
renderer.setSeriesPaint(i, colorForEachLabel(i));
}
}
@Override
public void displayRanking(Object ranking) {
Double[] values = null;
if (ranking != null) {
String[] alternatives = (String[]) ((Object[]) ranking)[0];
if (((Object[]) ranking)[1] instanceof int[]) {
values = new Double[((int[]) ((Object[]) ranking)[1]).length];
for (int i = 0; i < ((int[]) ((Object[]) ranking)[1]).length; ++i) {
values[i] = (double) ((int[]) ((Object[]) ranking)[1])[i];
}
} else if (((Object[]) ranking)[1] instanceof double[]) {
values = new Double[((double[]) ((Object[]) ranking)[1]).length];
for (int i = 0; i < ((double[]) ((Object[]) ranking)[1]).length; ++i) {
values[i] = (double) ((double[]) ((Object[]) ranking)[1])[i];
}
}
if (((Object[]) ranking).length == 2) {
displayAlternatives(alternatives, values, colors);
} else {
int[] pos = ((int[]) ((Object[]) ranking)[2]);
double[] alpha = ((double[]) ((Object[]) ranking)[3]);
displayAlternatives(alternatives, values, pos, alpha, colors);
}
}
}
public void displayAlternatives(String[] alternatives, Double[] pos, Color[] colors) {
if (_alternativesMarkers != null) {
for (ValueMarker marker : _alternativesMarkers) {
chart.getXYPlot().removeRangeMarker(marker);
}
}
_alternativesMarkers = null;
class MyItem implements Comparable<MyItem> {
public String alternative;
public Double pos;
public Color color;
public MyItem(String alternative, double pos, Color color) {
this.alternative = alternative;
this.pos = pos;
this.color = color;
}
@Override
public int compareTo(MyItem other) {
return Double.compare(this.pos, other.pos);
}
}
List<MyItem> items = null;
if ((alternatives != null) && (pos != null) && (colors != null)) {
items = new LinkedList<MyItem>();
for (int i = 0; i < pos.length; i++) {
if (alternatives[i] != null) {
items.add(new MyItem(alternatives[i], (Double) pos[i], colors[i]));
}
}
Collections.sort(items);
}
if (items != null) {
int size = items.size();
if (size > 0) {
int height = (int) (chartComposite.getSize().y * 0.75f);
int offset = height / size;
_alternativesMarkers = new ValueMarker[size];
MyItem item;
for (int i = 0; i < size; i++) {
item = items.get(i);
_alternativesMarkers[i] = new ValueMarker(item.pos);
_alternativesMarkers[i].setPaint(item.color);
_alternativesMarkers[i].setStroke(new BasicStroke(3));
_alternativesMarkers[i].setLabel(item.alternative);
_alternativesMarkers[i].setLabelFont(new Font("TimesRoman", Font.BOLD, 20)); //$NON-NLS-1$
_alternativesMarkers[i].setLabelPaint(item.color);
_alternativesMarkers[i].setLabelOffset(new RectangleInsets(offset * (i / offset), 25, 0, 0));
chart.getXYPlot().addRangeMarker(0, _alternativesMarkers[i], Layer.FOREGROUND);
}
}
}
}
public void displayAlternatives(String[] alternatives, Double[] values, int[] pos, double[] alpha, Color[] colors) {
displayAlternatives(alternatives, values, colors);
displayAlternatives(alternatives, pos, alpha);
}
@SuppressWarnings("unchecked")
public void displayAlternatives(String[] alternatives, int[] pos, double[] alpha) {
int size = alternatives.length;
int cardinality = ((FuzzySet) domain).getLabelSet().getCardinality();
XYSeries series, alternativeSeries;
double[] centers = new double[size];
double x, y, factor;
List<XYDataItem> dataItems;
for (int i = 0; i < size; i++) {
if (alternatives[i] != null) {
series = dataset.getSeries(pos[i]);
alternativeSeries = new XYSeries(alternatives[i]);
dataItems = (List<XYDataItem>) series.getItems();
if (alpha[i] >= 0) {
factor = (dataItems.get(3).getX().doubleValue() - dataItems.get(2).getX().doubleValue()) * alpha[i];
} else {
factor = (dataItems.get(1).getX().doubleValue() - dataItems.get(0).getX().doubleValue()) * alpha[i];
}
XYDataItem item;
for (int itemPos = 0; itemPos < dataItems.size(); itemPos++) {
item = dataItems.get(itemPos);
x = item.getXValue();
y = item.getYValue();
if (itemPos == 0) {
if (x == dataItems.get(1).getX().doubleValue()) {
x -= dataItems.get(3).getX().doubleValue() - dataItems.get(2).getX().doubleValue();
}
} else if (itemPos == 1) {
centers[i] = x + factor;
} else if (itemPos == 3) {
if (x == dataItems.get(2).getX().doubleValue()) {
x += dataItems.get(1).getX().doubleValue() - dataItems.get(0).getX().doubleValue();
}
}
alternativeSeries.add(x + factor, y);
}
dataset.addSeries(alternativeSeries);
}
}
chart.getXYPlot().setDataset(dataset);
XYPlot xyplot = (XYPlot) chart.getPlot();
XYItemRenderer renderer = xyplot.getRenderer(0);
XYTextAnnotation annotation;
for (int i = 0; i < xyplot.getSeriesCount(); i++) {
if (i >= cardinality) {
renderer.setSeriesStroke(i, new BasicStroke(3));
renderer.setSeriesPaint(i, colorForEachLabel(pos[i - cardinality]));
annotation = new XYTextAnnotation(alternatives[i - cardinality], centers[i - cardinality], 1.05);
annotation.setFont(new Font("SansSerif", Font.PLAIN, 10)); //$NON-NLS-1$
xyplot.addAnnotation(annotation);
} else {
renderer.setSeriesStroke(i, new BasicStroke(1));
renderer.setSeriesPaint(i, colorForEachLabel(i));
}
}
}
public void displayAlternatives(String[] alternatives, TrapezoidalFunction[] fuzzyNumbers) {
int size = fuzzyNumbers.length;
XYSeries alternativeSeries = null;
for (int i = 0; i < size; i++) {
alternativeSeries = new XYSeries(alternatives[i]);
alternativeSeries.add(fuzzyNumbers[i].getLimits()[0], 0);
alternativeSeries.add(fuzzyNumbers[i].getLimits()[1], 1);
alternativeSeries.add(fuzzyNumbers[i].getLimits()[2], 1);
alternativeSeries.add(fuzzyNumbers[i].getLimits()[3], 0);
dataset.addSeries(alternativeSeries);
}
chart.getXYPlot().setDataset(dataset);
XYPlot xyplot = (XYPlot) chart.getPlot();
XYItemRenderer renderer = xyplot.getRenderer(0);
for (int i = 0; i < xyplot.getSeriesCount(); i++) {
if (i >= ((FuzzySet) domain).getLabelSet().getCardinality()) {
renderer.setSeriesStroke(i, new BasicStroke(3));
} else {
renderer.setSeriesStroke(i, new BasicStroke());
renderer.setSeriesPaint(i, colorForEachLabel(i));
}
}
}
}
@@ -0,0 +1,261 @@
package afryca.domain.gui.fuzzyset.dialog;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.e4.core.services.nls.Translation;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.services.IStylingEngine;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;
import afryca.domain.Domain;
import afryca.domain.fuzzyset.FuzzySet;
import afryca.domain.gui.dialog.DomainDialog;
import afryca.domain.gui.fuzzy.l10n.Messages;
import afryca.domain.gui.fuzzyset.chart.FuzzyChart;
import afryca.domain.gui.fuzzyset.icons.Icons;
public class FuzzyDialog extends DomainDialog {
private static final String PLUGIN_ID = "afryca.domain.gui.fuzzyset"; //$NON-NLS-1$
private static final int BUTTON_OK = IDialogConstants.OK_ID;
private static final int CHART_WIDTH = 610;
private static final int CHART_HEIGHT = 250;
private static final int TEXT_HEIGHT = 25;
private static final int HORIZONTAL_MARGIN = 90;
private static final int DIALOG_WIDTH = CHART_WIDTH + HORIZONTAL_MARGIN;
private static final int DIALOG_HEIGHT = TEXT_HEIGHT * 4 + CHART_HEIGHT * 2;
private FuzzyChart chart;
private Composite container;
private Text labelsText;
private Text textId;
private ControlDecoration labelsControlDecoration;
private ControlDecoration domainControlDecoration;
@Translation
@Inject
private Messages messages;
@Translation
@Inject
private Icons icons;
@Inject
private IStylingEngine engine;
@Inject
@Named(DOMAINS)
private List<Domain> domains;
private List<String> ids;
public FuzzyDialog() {
super();
}
@Inject
public FuzzyDialog(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
super(shell);
}
@Override
protected Control createDialogArea(Composite parent) {
container = (Composite) super.createDialogArea(parent);
GridLayoutFactory.swtDefaults().applyTo(container);
styleDialog();
initialize();
createControls();
addListeners();
return container;
}
private void styleDialog() {
engine.setClassname(getShell(), DIALOG_CLASS_SHELL);
getShell().setText(messages.create_fuzzy_set);
setTitleImage(ResourceManager.getPluginImage(PLUGIN_ID, icons.domain));
setTitle(messages.create_fuzzy_set);
setMessage(messages.insert_labels_with_separator_colon, SWT.NONE);
}
private void initialize() {
domain = new FuzzySet();
id = ""; //$NON-NLS-1$
ids = new ArrayList<String>();
for(Domain d: domains) {
ids.add(d.getId());
}
}
private void createControls() {
Label idLabel = new Label(container, SWT.NULL);
idLabel.setText(messages.domain_name);
GridData gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1);
idLabel.setLayoutData(gridData);
textId = new Text(container, SWT.BORDER);
textId.setText(id);
textId.setFocus();
gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1);
setGridDataHint(gridData);
textId.setLayoutData(gridData);
Label insertLabelsLabel = new Label(container, SWT.NULL);
gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false, 4, 1);
insertLabelsLabel.setLayoutData(gridData);
insertLabelsLabel.setText(messages.labels);
labelsText = new Text(container, SWT.BORDER);
gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1);
setGridDataHint(gridData);
labelsText.setLayoutData(gridData);
labelsControlDecoration = createNotificationDecorator(labelsText);
domainControlDecoration = createNotificationDecorator(textId);
createChart();
}
private void setGridDataHint(GridData gridData) {
gridData.widthHint = CHART_WIDTH;
gridData.heightHint = TEXT_HEIGHT;
}
private void createChart() {
Composite composite = new Composite(container, SWT.NONE);
composite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
chart = new FuzzyChart();
chart.initialize(domain, composite, CHART_WIDTH, CHART_HEIGHT, SWT.BORDER);
}
private void addListeners() {
textId.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
id = ((Text) e.getSource()).getText().trim();
domain.setId(id);
configureButtons();
validate();
}
});
labelsText.addListener(SWT.Verify, new Listener() {
public void handleEvent(Event e) {
if(e.character == ' ') {
String before = labelsText.getText().substring(labelsText.getText().length() - 1);
if(before.equals(":")) { //$NON-NLS-1$
e.doit = false;
return;
}
}
configureButtons();
validate();
}
});
labelsText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
if(!labelsText.getText().isEmpty()) {
if(!checkRepitedLabels()) {
((FuzzySet) domain).createTrapezoidalFunction(labelsText.getText().split(":")); //$NON-NLS-1$
}
}
configureButtons();
chart.setDomain(domain);
validate();
}
});
}
private boolean checkRepitedLabels() {
boolean duplicate = false;
List<String> aux = new ArrayList<>();
for (int i = 0; i < labelsText.getText().split(":").length; i++) { //$NON-NLS-1$
if(aux.contains(labelsText.getText().split(":")[i])) { //$NON-NLS-1$
duplicate = true;
}
aux.add(labelsText.getText().split(":")[i]); //$NON-NLS-1$
}
return duplicate;
}
private void configureButtons() {
Button okButton = getButton(BUTTON_OK);
if (okButton != null) {
okButton.setEnabled(validate());
}
}
private boolean validate() {
String msgLabel = "", msgId = ""; //$NON-NLS-1$ //$NON-NLS-2$
if(labelsText.getText().isEmpty() ) {
msgLabel = messages.empty_domain;
}
if(labelsText.getText().split(":").length < 3) { //$NON-NLS-1$
msgLabel = messages.at_least_3_labels_are_required;
}
if(checkRepitedLabels()) {
msgLabel = messages.duplicate_label;
}
if(id.isEmpty()) {
msgId = messages.empty_value;
}
if(ids.contains(id)) {
msgId = messages.duplicate_name;
}
boolean labelB = validate(labelsControlDecoration, msgLabel);
boolean idB = validate(domainControlDecoration, msgId);
return labelB && idB;
}
@Override
protected Point getInitialSize() {
return new Point(DIALOG_WIDTH, DIALOG_HEIGHT);
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, BUTTON_OK,IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
configureButtons();
showDecoration();
}
private void showDecoration() {
labelsControlDecoration.show();
domainControlDecoration.show();
}
}
@@ -0,0 +1,11 @@
package afryca.domain.gui.fuzzyset.icons;
/**
* Localized icons
*
* @author Sinbad²
* @version 3.0
*/
public class Icons {
public String domain;
}
@@ -0,0 +1 @@
domain=icons/fuzzy.png