grammar org.openhab.model.rule.Rules with org.openhab.model.script.Script

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
import "http://www.openhab.org/model/Items"
import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

generate rules "http://www.openhab.org/model/rule/Rules"

RuleModel:
	(imports += Import)*
	(variables+=XVariableDeclaration)*
	(rules += Rule)*;
	
Import:
	'import' importedNamespace=QualifiedNameWithWildCard
;
	
//Variable:
//	'var' type=[types::JvmTypeReference|QualifiedName] name=ID
//;
	
Rule:
	'rule' name=(ID|STRING)
	'when' eventtrigger+=EventTrigger ('or' eventtrigger+=EventTrigger)*
	'then' script=Script
	'end'
;

EventTrigger:
	UpdateEventTrigger |
	CommandEventTrigger |
	ChangedEventTrigger |
	TimerTrigger |
	SystemTrigger
;

CommandEventTrigger:
	'Item' item=ItemName 'received command' (command=ValidCommand)?
;

UpdateEventTrigger:
	'Item' item=ItemName 'received update' (state=ValidState)?
;

ChangedEventTrigger:
	'Item' item=ItemName 'changed' ('from' oldState=ValidState)? ('to' newState=ValidState)?
;		

TimerTrigger:
	'Time' 'cron' cron=STRING |
	'Time' 'is' time=('midnight' | 'noon')
;

SystemTrigger:
	SystemOnStartupTrigger |
	SystemOnShutdownTrigger
;

SystemOnStartupTrigger:
	{SystemOnStartupTrigger}
	'System' 'started'
;

SystemOnShutdownTrigger:
	{SystemOnShutdownTrigger}
	'System' 'shuts down'
;

QualifiedNameWithWildCard :
	QualifiedName  ('.' '*')?;
	
ItemName :
	ID
;

ValidState:
	ID | INT | Number | STRING
;

ValidCommand:
	ID | INT | Number | STRING
;
