use of org.eclipse.xtext.example.homeautomation.ruleEngine.Rule in project xtext-eclipse by eclipse.
the class RuleEngineValidator method checkRuleRecursion.
@Check
public void checkRuleRecursion(final XFeatureCall featureCall) {
final Rule containingRule = EcoreUtil2.<Rule>getContainerOfType(featureCall, Rule.class);
if (((((containingRule != null) && (featureCall.getFeature() instanceof JvmOperation)) && Objects.equal(featureCall.getConcreteSyntaxFeatureName(), "fire")) && (featureCall.getFeatureCallArguments().size() == 1))) {
final XExpression argument = IterableExtensions.<XExpression>head(featureCall.getFeatureCallArguments());
if ((argument instanceof XAbstractFeatureCall)) {
final EObject sourceElem = this._iJvmModelAssociations.getPrimarySourceElement(((XAbstractFeatureCall) argument).getFeature());
org.eclipse.xtext.example.homeautomation.ruleEngine.State _deviceState = containingRule.getDeviceState();
boolean _equals = Objects.equal(sourceElem, _deviceState);
if (_equals) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Firing the same device state that triggers the rule \"");
String _description = containingRule.getDescription();
_builder.append(_description);
_builder.append("\" may lead to endless recursion.");
this.warning(_builder.toString(), featureCall, XbasePackage.Literals.XFEATURE_CALL__FEATURE_CALL_ARGUMENTS, 0);
}
}
}
}
use of org.eclipse.xtext.example.homeautomation.ruleEngine.Rule in project xtext-eclipse by eclipse.
the class ModelTest method testParsing.
@Test
public void testParsing() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Device Window can be open, closed");
_builder.newLine();
_builder.append("Device Heater can be on, off");
_builder.newLine();
_builder.append("Rule \"Save energy\" when Window.open then");
_builder.newLine();
_builder.append("\t");
_builder.append("fire(Heater.off)");
_builder.newLine();
_builder.append("\t");
_builder.append("println(\'Another penny to the piggy bank!\')");
_builder.newLine();
final Model model = this._parseHelper.parse(_builder);
Declaration _get = model.getDeclarations().get(0);
final Device device = ((Device) _get);
Assert.assertEquals("Window", device.getName());
Declaration _get_1 = model.getDeclarations().get(2);
final Rule rule = ((Rule) _get_1);
Assert.assertEquals("Save energy", rule.getDescription());
Assert.assertEquals("open", rule.getDeviceState().getName());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.example.homeautomation.ruleEngine.Rule in project xtext-eclipse by eclipse.
the class SerializerTest method test.
@Test
public void test() {
try {
final Model model = this._ruleEngineFactory.createModel();
final XtextResourceSet rs = this.rsProvider.get();
EList<Declaration> _declarations = model.getDeclarations();
Device _createDevice = this._ruleEngineFactory.createDevice();
final Procedure1<Device> _function = (Device it) -> {
it.setName("Window");
EList<State> _states = it.getStates();
final Function1<String, State> _function_1 = (String s) -> {
State _createState = this._ruleEngineFactory.createState();
final Procedure1<State> _function_2 = (State it_1) -> {
it_1.setName(s);
};
return ObjectExtensions.<State>operator_doubleArrow(_createState, _function_2);
};
List<State> _map = ListExtensions.<String, State>map(Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("open", "closed")), _function_1);
Iterables.<State>addAll(_states, _map);
};
Device _doubleArrow = ObjectExtensions.<Device>operator_doubleArrow(_createDevice, _function);
_declarations.add(_doubleArrow);
EList<Declaration> _declarations_1 = model.getDeclarations();
Device _createDevice_1 = this._ruleEngineFactory.createDevice();
final Procedure1<Device> _function_1 = (Device it) -> {
it.setName("Heater");
EList<State> _states = it.getStates();
final Function1<String, State> _function_2 = (String s) -> {
State _createState = this._ruleEngineFactory.createState();
final Procedure1<State> _function_3 = (State it_1) -> {
it_1.setName(s);
};
return ObjectExtensions.<State>operator_doubleArrow(_createState, _function_3);
};
List<State> _map = ListExtensions.<String, State>map(Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("on", "off")), _function_2);
Iterables.<State>addAll(_states, _map);
};
Device _doubleArrow_1 = ObjectExtensions.<Device>operator_doubleArrow(_createDevice_1, _function_1);
_declarations_1.add(_doubleArrow_1);
EList<Declaration> _declarations_2 = model.getDeclarations();
Rule _createRule = this._ruleEngineFactory.createRule();
final Procedure1<Rule> _function_2 = (Rule it) -> {
it.setDescription("Save energy");
Declaration _head = IterableExtensions.<Declaration>head(model.getDeclarations());
it.setDeviceState(IterableExtensions.<State>head(((Device) _head).getStates()));
XBlockExpression _createXBlockExpression = this._xbaseFactory.createXBlockExpression();
final Procedure1<XBlockExpression> _function_3 = (XBlockExpression it_1) -> {
EList<XExpression> _expressions = it_1.getExpressions();
XVariableDeclaration _createXVariableDeclaration = this._xbaseFactory.createXVariableDeclaration();
final Procedure1<XVariableDeclaration> _function_4 = (XVariableDeclaration it_2) -> {
it_2.setName("msg");
XStringLiteral _createXStringLiteral = this._xbaseFactory.createXStringLiteral();
final Procedure1<XStringLiteral> _function_5 = (XStringLiteral it_3) -> {
it_3.setValue("Another penny to the piggy bank!");
};
XStringLiteral _doubleArrow_2 = ObjectExtensions.<XStringLiteral>operator_doubleArrow(_createXStringLiteral, _function_5);
it_2.setRight(_doubleArrow_2);
};
XVariableDeclaration _doubleArrow_2 = ObjectExtensions.<XVariableDeclaration>operator_doubleArrow(_createXVariableDeclaration, _function_4);
_expressions.add(_doubleArrow_2);
EList<XExpression> _expressions_1 = it_1.getExpressions();
XVariableDeclaration _createXVariableDeclaration_1 = this._xbaseFactory.createXVariableDeclaration();
final Procedure1<XVariableDeclaration> _function_5 = (XVariableDeclaration it_2) -> {
it_2.setName("x");
XNumberLiteral _createXNumberLiteral = this._xbaseFactory.createXNumberLiteral();
final Procedure1<XNumberLiteral> _function_6 = (XNumberLiteral it_3) -> {
it_3.setValue("1");
};
XNumberLiteral _doubleArrow_3 = ObjectExtensions.<XNumberLiteral>operator_doubleArrow(_createXNumberLiteral, _function_6);
it_2.setRight(_doubleArrow_3);
};
XVariableDeclaration _doubleArrow_3 = ObjectExtensions.<XVariableDeclaration>operator_doubleArrow(_createXVariableDeclaration_1, _function_5);
_expressions_1.add(_doubleArrow_3);
};
XBlockExpression _doubleArrow_2 = ObjectExtensions.<XBlockExpression>operator_doubleArrow(_createXBlockExpression, _function_3);
it.setThenPart(_doubleArrow_2);
};
Rule _doubleArrow_2 = ObjectExtensions.<Rule>operator_doubleArrow(_createRule, _function_2);
_declarations_2.add(_doubleArrow_2);
final Resource resource = rs.createResource(URI.createURI("heater.rules"));
EList<EObject> _contents = resource.getContents();
_contents.add(model);
final StringWriter sw = new StringWriter();
this.serializer.serialize(model, sw, SaveOptions.newBuilder().format().getOptions());
StringConcatenation _builder = new StringConcatenation();
_builder.append("Device Window can be open, closed");
_builder.newLine();
_builder.append("Device Heater can be on, off");
_builder.newLine();
_builder.append("Rule \"Save energy\" when Window.open then");
_builder.newLine();
_builder.append("\t");
_builder.append("val msg = \"Another penny to the piggy bank!\"");
_builder.newLine();
_builder.append("\t");
_builder.append("val x = 1");
_builder.newLine();
Assert.assertEquals(_builder.toString(), sw.toString());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.example.homeautomation.ruleEngine.Rule in project xtext-eclipse by eclipse.
the class RuleEngineJvmModelInferrer method _infer.
protected void _infer(final Model element, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
final String className = element.eResource().getURI().trimFileExtension().lastSegment();
final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
Iterable<Rule> _filter = Iterables.<Rule>filter(element.getDeclarations(), Rule.class);
for (final Rule device : _filter) {
EList<JvmMember> _members = it.getMembers();
final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
it_1.setStatic(true);
this._jvmTypesBuilder.setBody(it_1, device.getThenPart());
};
JvmOperation _method = this._jvmTypesBuilder.toMethod(device, RuleEngineJvmModelInferrer.getRuleMethodName(device), this._typeReferenceBuilder.typeRef(void.class), _function_1);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
}
EList<JvmMember> _members_1 = it.getMembers();
final Procedure1<JvmOperation> _function_2 = (JvmOperation it_1) -> {
it_1.setStatic(true);
EList<JvmFormalParameter> _parameters = it_1.getParameters();
JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(element, "event", this._typeReferenceBuilder.typeRef(Object.class));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
{
Iterable<Device> _filter = Iterables.<Device>filter(element.getDeclarations(), Device.class);
for (final Device device : _filter) {
{
EList<State> _states = device.getStates();
for (final State state : _states) {
_builder.append("if (event == ");
String _qualifiedJavaName = RuleEngineJvmModelInferrer.this.getQualifiedJavaName(state);
_builder.append(_qualifiedJavaName);
_builder.append(") {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("System.out.println(\"");
String _name = device.getName();
_builder.append(_name, "\t");
_builder.append(" is now ");
String _name_1 = state.getName();
_builder.append(_name_1, "\t");
_builder.append("!\");");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
}
}
}
}
{
Iterable<Rule> _filter_1 = Iterables.<Rule>filter(element.getDeclarations(), Rule.class);
for (final Rule rule : _filter_1) {
_builder.append("if (event == ");
String _qualifiedJavaName_1 = RuleEngineJvmModelInferrer.this.getQualifiedJavaName(rule.getDeviceState());
_builder.append(_qualifiedJavaName_1);
_builder.append(") {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
String _ruleMethodName = RuleEngineJvmModelInferrer.getRuleMethodName(rule);
_builder.append(_ruleMethodName, "\t");
_builder.append("();");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
}
}
}
};
this._jvmTypesBuilder.setBody(it_1, _client);
};
JvmOperation _method_1 = this._jvmTypesBuilder.toMethod(element, "fire", this._typeReferenceBuilder.typeRef(void.class), _function_2);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members_1, _method_1);
EList<JvmMember> _members_2 = it.getMembers();
final Procedure1<JvmOperation> _function_3 = (JvmOperation it_1) -> {
it_1.setStatic(true);
EList<JvmFormalParameter> _parameters = it_1.getParameters();
JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(element, "args", this._jvmTypesBuilder.addArrayTypeDimension(this._typeReferenceBuilder.typeRef(String.class)));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append(Scanner.class);
_builder.append(" scanner = new Scanner(System.in);");
_builder.newLineIfNotEmpty();
_builder.append("System.out.println(\"Welcome home!\");");
_builder.newLine();
_builder.append("System.out.println(\"Available commands : \");");
_builder.newLine();
{
Iterable<Device> _filter = Iterables.<Device>filter(element.getDeclarations(), Device.class);
for (final Device device : _filter) {
{
EList<State> _states = device.getStates();
for (final State state : _states) {
_builder.append("System.out.println(\" ");
String _name = device.getName();
_builder.append(_name);
_builder.append(" ");
String _name_1 = state.getName();
_builder.append(_name_1);
_builder.append("\" );");
_builder.newLineIfNotEmpty();
}
}
}
}
_builder.append("System.out.println(\"Have fun!\");");
_builder.newLine();
_builder.append("while(true) {");
_builder.newLine();
_builder.append("\t");
_builder.append("String command = scanner.next();");
_builder.newLine();
{
Iterable<Device> _filter_1 = Iterables.<Device>filter(element.getDeclarations(), Device.class);
for (final Device device_1 : _filter_1) {
_builder.append("\t");
_builder.append("if (command.equalsIgnoreCase(\"");
String _name_2 = device_1.getName();
_builder.append(_name_2, "\t");
_builder.append("\")) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("String secondaryCommand = scanner.next();");
_builder.newLine();
{
EList<State> _states_1 = device_1.getStates();
for (final State state_1 : _states_1) {
_builder.append("\t");
_builder.append("\t");
_builder.append("if (secondaryCommand.equalsIgnoreCase(\"");
String _name_3 = state_1.getName();
_builder.append(_name_3, "\t\t");
_builder.append("\")) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("\t");
_builder.append("fire(");
String _qualifiedJavaName = RuleEngineJvmModelInferrer.this.getQualifiedJavaName(state_1);
_builder.append(_qualifiedJavaName, "\t\t\t");
_builder.append(");");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("} else ");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("\t");
_builder.append("{");
_builder.newLine();
_builder.append("\t");
_builder.append("\t\t");
_builder.append("System.out.println(\"");
String _name_4 = device_1.getName();
_builder.append(_name_4, "\t\t\t");
_builder.append(" can only have the following states: ");
final Function1<State, String> _function = (State it_2) -> {
return it_2.getName();
};
String _join = IterableExtensions.join(ListExtensions.<State, String>map(device_1.getStates(), _function), ",");
_builder.append(_join, "\t\t\t");
_builder.append(".\");");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("if (command.equalsIgnoreCase(\"bye\")) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("System.out.println(\"Ciao!\");");
_builder.newLine();
_builder.append("\t\t");
_builder.append("break;");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
};
this._jvmTypesBuilder.setBody(it_1, _client);
};
JvmOperation _method_2 = this._jvmTypesBuilder.toMethod(element, "main", this._typeReferenceBuilder.typeRef(void.class), _function_3);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members_2, _method_2);
};
acceptor.<JvmGenericType>accept(this._jvmTypesBuilder.toClass(element, className), _function);
Iterable<Device> _filter = Iterables.<Device>filter(element.getDeclarations(), Device.class);
for (final Device device : _filter) {
final Procedure1<JvmEnumerationType> _function_1 = (JvmEnumerationType it) -> {
};
final Procedure1<JvmEnumerationType> _function_2 = (JvmEnumerationType it) -> {
EList<State> _states = device.getStates();
for (final State state : _states) {
EList<JvmMember> _members = it.getMembers();
final Procedure1<JvmEnumerationLiteral> _function_3 = (JvmEnumerationLiteral it_1) -> {
it_1.setVisibility(JvmVisibility.PUBLIC);
};
JvmEnumerationLiteral _enumerationLiteral = this._jvmTypesBuilder.toEnumerationLiteral(state, state.getName(), _function_3);
this._jvmTypesBuilder.<JvmEnumerationLiteral>operator_add(_members, _enumerationLiteral);
}
};
acceptor.<JvmEnumerationType>accept(this._jvmTypesBuilder.toEnumerationType(device, device.getName(), _function_1), _function_2);
}
}
use of org.eclipse.xtext.example.homeautomation.ruleEngine.Rule in project xtext-eclipse by eclipse.
the class RuleEngineValidator method checkUniqueDeclarations.
@Check
public void checkUniqueDeclarations(final Model model) {
final HashSet<String> deviceNames = CollectionLiterals.<String>newHashSet();
final HashSet<String> ruleDescriptions = CollectionLiterals.<String>newHashSet();
EList<Declaration> _declarations = model.getDeclarations();
for (final Declaration decl : _declarations) {
if ((decl instanceof Device)) {
boolean _add = deviceNames.add(((Device) decl).getName());
boolean _not = (!_add);
if (_not) {
this.error("Device names must be unique.", decl, RuleEnginePackage.Literals.DEVICE__NAME);
}
} else {
if ((decl instanceof Rule)) {
final String methodName = RuleEngineJvmModelInferrer.getRuleMethodName(((Rule) decl));
boolean _add_1 = ruleDescriptions.add(methodName);
boolean _not_1 = (!_add_1);
if (_not_1) {
this.error("Rule descriptions must be unique.", decl, RuleEnginePackage.Literals.RULE__DESCRIPTION);
}
}
}
}
}
Aggregations