Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved the value field of Value in Constant and renamed Value into Expression #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import fr.obeo.dsl.arduino.Constant;
import fr.obeo.dsl.arduino.Control;
import fr.obeo.dsl.arduino.DigitalPin;
import fr.obeo.dsl.arduino.Expression;
import fr.obeo.dsl.arduino.Function;
import fr.obeo.dsl.arduino.FunctionCall;
import fr.obeo.dsl.arduino.Hardware;
Expand All @@ -63,7 +64,6 @@
import fr.obeo.dsl.arduino.Set;
import fr.obeo.dsl.arduino.Sketch;
import fr.obeo.dsl.arduino.Status;
import fr.obeo.dsl.arduino.Value;
import fr.obeo.dsl.arduino.Variable;
import fr.obeo.dsl.arduino.While;

Expand Down Expand Up @@ -312,18 +312,18 @@ public String computeLevelLabel(fr.obeo.dsl.arduino.Level level) {
String label = level.getModule().getName();
if (level.getLevel() != null) {
label += "=";
Value value = level.getLevel();
Expression value = level.getLevel();
label += computeLabel(value);
}
return label;
}

public String computeLabel(Value value) {
public String computeLabel(Expression value) {
if (value instanceof Variable) {
return ((Variable) value).getName();
}
if (value instanceof Constant) {
return value.getValue();
return ((Constant) value).getValue();
}
if (value instanceof Sensor) {
return ((Sensor) value).getModule().getName();
Expand Down Expand Up @@ -482,27 +482,26 @@ public OperatorKind getOperator(String operator) {
return null;
}

public Value getValue(Sketch sketch, String value) {
public Expression getExpression(Sketch sketch, String expression) {
for (Instruction instruction : sketch.getInstructions()) {
if (instruction instanceof Value
&& value.equals(((Value) instruction).getValue())) {
return (Value) instruction;
if (instruction instanceof Constant
&& expression.equals(((Constant) instruction).getValue())) {
return (Constant) instruction;
}
if (instruction instanceof Variable
&& value.equals(((Variable) instruction).getName())) {
return (Value) instruction;
&& expression.equals(((Variable) instruction).getName())) {
return (Expression) instruction;
}
}
if (isInteger(value)) {
if (isInteger(expression)) {
fr.obeo.dsl.arduino.Constant constant = ArduinoFactory.eINSTANCE
.createConstant();
constant.setValue(value);
constant.setValue(expression);
sketch.getInstructions().add(constant);
return constant;
}
Variable var = ArduinoFactory.eINSTANCE.createVariable();
var.setName(value);
var.setValue("0");
var.setName(expression);
sketch.getInstructions().add(var);
return var;
}
Expand All @@ -525,54 +524,54 @@ public void editLabel(While instruction, Sketch sketch, String left,
instruction.setCondition(condition);
}

Value oldLeft = condition.getLeft();
Value oldRight = condition.getRight();
Expression oldLeft = condition.getLeft();
Expression oldRight = condition.getRight();

condition.setLeft(getValue(sketch, left));
condition.setLeft(getExpression(sketch, left));
condition.setOperator(getOperator(operator));
condition.setRight(getValue(sketch, right));
condition.setRight(getExpression(sketch, right));

deleteUnusedValue(sketch, oldLeft);
deleteUnusedValue(sketch, oldRight);
deleteUnusedExpression(sketch, oldLeft);
deleteUnusedExpression(sketch, oldRight);
}

public void editLabel(Set instruction, Sketch sketch, String variable,
String value) {
Value oldVariable = instruction.getVariable();
Value oldValue = instruction.getValue();
instruction.setVariable((Variable) getValue(sketch, variable));
instruction.setValue(getValue(sketch, value));
Expression oldVariable = instruction.getVariable();
Expression oldValue = instruction.getValue();
instruction.setVariable((Variable) getExpression(sketch, variable));
instruction.setValue(getExpression(sketch, value));

// Clean unused values
deleteUnusedValue(sketch, oldVariable);
deleteUnusedValue(sketch, oldValue);
deleteUnusedExpression(sketch, oldVariable);
deleteUnusedExpression(sketch, oldValue);
}

public void editLabel(Value value, String newValue) {
public void editLabel(Expression value, String newValue) {

}

public void deleteUnusedValues(Sketch sketch) {
public void deleteUnusedExpressions(Sketch sketch) {
ImmutableList<Instruction> instructions = ImmutableList.copyOf(sketch
.getInstructions());
for (Instruction instruction : instructions) {
if (instruction instanceof Value) {
deleteUnusedValue(sketch, (Value) instruction);
if (instruction instanceof Expression) {
deleteUnusedExpression(sketch, (Expression) instruction);
}
}
}

private void deleteUnusedValue(Sketch sketch, Value value) {
if (value != null && isNotUsedAnymore(sketch, value)) {
EcoreUtil.delete(value);
private void deleteUnusedExpression(Sketch sketch, Expression expression) {
if (expression != null && isNotUsedAnymore(sketch, expression)) {
EcoreUtil.delete(expression);
}
}

private boolean isNotUsedAnymore(Sketch sketch, Value value) {
ResourceSet resourceSet = value.eResource().getResourceSet();
private boolean isNotUsedAnymore(Sketch sketch, Expression expression) {
ResourceSet resourceSet = expression.eResource().getResourceSet();
ECrossReferenceAdapter adapter = new ECrossReferenceAdapter();
resourceSet.eAdapters().add(adapter);
Collection<Setting> refs = adapter.getInverseReferences(value, true);
Collection<Setting> refs = adapter.getInverseReferences(expression, true);
return refs.size() == 1;
}

Expand All @@ -581,8 +580,8 @@ public List<Variable> getVariables(EObject container) {
if (container instanceof Set) {
variables.add(((Set) container).getVariable());
} else if (container instanceof MathOperator) {
Value left = ((MathOperator) container).getLeft();
Value right = ((MathOperator) container).getRight();
Expression left = ((MathOperator) container).getLeft();
Expression right = ((MathOperator) container).getRight();
if (left instanceof Variable) {
variables.add((Variable) left);
}
Expand All @@ -596,13 +595,13 @@ public List<Variable> getVariables(EObject container) {
public List<NumericalOperator> getNumericalOperators(EObject container) {
List<NumericalOperator> operators = Lists.newArrayList();
if (container instanceof Set) {
Value value = ((Set) container).getValue();
Expression value = ((Set) container).getValue();
if (value instanceof NumericalOperator) {
operators.add((NumericalOperator) value);
}
} else if (container instanceof MathOperator) {
Value left = ((MathOperator) container).getLeft();
Value right = ((MathOperator) container).getRight();
Expression left = ((MathOperator) container).getLeft();
Expression right = ((MathOperator) container).getRight();
if (left instanceof NumericalOperator) {
operators.add((NumericalOperator) left);
}
Expand Down Expand Up @@ -702,13 +701,13 @@ public List<BooleanOperator> getBooleanOperators(EObject container) {
public List<Constant> getConstants(EObject container) {
List<Constant> constants = Lists.newArrayList();
if (container instanceof Set) {
Value value = ((Set) container).getValue();
Expression value = ((Set) container).getValue();
if (value instanceof Constant) {
constants.add((Constant) value);
}
} else if (container instanceof MathOperator) {
Value left = ((MathOperator) container).getLeft();
Value right = ((MathOperator) container).getRight();
Expression left = ((MathOperator) container).getLeft();
Expression right = ((MathOperator) container).getRight();
if (left instanceof Constant) {
constants.add((Constant) left);
}
Expand Down Expand Up @@ -851,12 +850,12 @@ public String getImage(ModuleInstruction instruction) {
+ instruction.getModule().getImage();
}

public void updateValue(Level level, String newValue) {
newValue = newValue.trim();
public void updateExpression(Level level, String newExpression) {
newExpression = newExpression.trim();
Sketch sketch = getSketch(level);
Value value = getValue(sketch, newValue);
Expression value = getExpression(sketch, newExpression);
level.setLevel(value);
deleteUnusedValues(sketch);
deleteUnusedExpressions(sketch);
}

public void addVariable(Instruction container, Variable variable) {
Expand All @@ -865,7 +864,7 @@ public void addVariable(Instruction container, Variable variable) {
} else if (container instanceof Set) {
((Set) container).setVariable(variable);
}
deleteUnusedValues(getSketch(variable));
deleteUnusedExpressions(getSketch(variable));
}

public void addValue(Instruction container, Constant value) {
Expand All @@ -874,12 +873,12 @@ public void addValue(Instruction container, Constant value) {
} else if (container instanceof Set) {
((Set) container).setValue(value);
}
deleteUnusedValues(getSketch(value));
deleteUnusedExpressions(getSketch(value));
}

private void addMathOperatorValue(MathOperator container, Value value) {
Value left = container.getLeft();
Value right = container.getRight();
private void addMathOperatorValue(MathOperator container, Expression value) {
Expression left = container.getLeft();
Expression right = container.getRight();

if (left == null && right == null) {
container.setLeft(value);
Expand All @@ -890,30 +889,30 @@ private void addMathOperatorValue(MathOperator container, Value value) {
} else if (left != null && right != null) {
container.setLeft(value);
}
deleteUnusedValues(getSketch(value));
deleteUnusedExpressions(getSketch(value));
}

public void updateValue(Instruction container, Value newValue,
Value oldValue) {
public void updateExpression(Instruction container, Expression newExpression,
Expression oldExpression) {
if (container instanceof Set) {
if (newValue instanceof Variable) {
((Set) container).setVariable((Variable) newValue);
if (newExpression instanceof Variable) {
((Set) container).setVariable((Variable) newExpression);
} else {
((Set) container).setValue(newValue);
((Set) container).setValue(newExpression);
}
} else if (container instanceof MathOperator) {
Value left = ((MathOperator) container).getLeft();
Value right = ((MathOperator) container).getRight();
if (oldValue.equals(left)) {
((MathOperator) container).setLeft(newValue);
} else if (oldValue.equals(right)) {
((MathOperator) container).setRight(newValue);
Expression left = ((MathOperator) container).getLeft();
Expression right = ((MathOperator) container).getRight();
if (oldExpression.equals(left)) {
((MathOperator) container).setLeft(newExpression);
} else if (oldExpression.equals(right)) {
((MathOperator) container).setRight(newExpression);
}

} else if (container instanceof Level) {
((Level) container).setLevel(newValue);
((Level) container).setLevel(newExpression);
}
deleteUnusedValues(getSketch(container));
deleteUnusedExpressions(getSketch(container));
}

public void openHardwareDiagram(Hardware hardware) {
Expand Down
3 changes: 3 additions & 0 deletions plugins/fr.obeo.dsl.arduino.edit/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,6 @@ _UI_ParameterDefinition_name_feature = Name
_UI_ParameterCall_type = Parameter Call
_UI_ParameterCall_definition_feature = Definition
_UI_Parameter_definition_feature = Definition
_UI_Expression_type = Expression
_UI_Constant_value_feature = Value
_UI_Library_music_literal = music
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@
* @generated
*/
public class AnalogPinItemProvider
extends PinItemProvider
implements
IEditingDomainItemProvider,
IStructuredItemContentProvider,
ITreeItemContentProvider,
IItemLabelProvider,
IItemPropertySource {
extends PinItemProvider {
/**
* This constructs an instance from a factory and a notifier.
* <!-- begin-user-doc -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import fr.obeo.dsl.arduino.BooleanOperator;

import fr.obeo.dsl.arduino.OperatorKind;
import java.util.Collection;
import java.util.List;

Expand All @@ -33,13 +34,7 @@
* @generated
*/
public class BooleanOperatorItemProvider
extends MathOperatorItemProvider
implements
IEditingDomainItemProvider,
IStructuredItemContentProvider,
ITreeItemContentProvider,
IItemLabelProvider,
IItemPropertySource {
extends MathOperatorItemProvider {
/**
* This constructs an instance from a factory and a notifier.
* <!-- begin-user-doc -->
Expand Down Expand Up @@ -84,7 +79,8 @@ public Object getImage(Object object) {
*/
@Override
public String getText(Object object) {
String label = ((BooleanOperator)object).getValue();
OperatorKind labelValue = ((BooleanOperator)object).getOperator();
String label = labelValue == null ? null : labelValue.toString();
return label == null || label.length() == 0 ?
getString("_UI_BooleanOperator_type") :
getString("_UI_BooleanOperator_type") + " " + label;
Expand Down
Loading