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

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

generate rules "https://openhab.org/model/Rules"

RuleModel:
	importSection = XImportSection?
	(variables+=VariableDeclaration)*
	(rules += Rule)*;
	
//Import:
//	'import' importedNamespace=QualifiedNameWithWildcard
//;
		
VariableDeclaration:
	(writeable?='var'|'val') (=>(type=JvmTypeReference name=ValidID) | name=ValidID) ('=' right=XExpression)?
;

Rule:
	'rule' name=(STRING|ID)
	'when' eventtrigger+=EventTrigger ('or' eventtrigger+=EventTrigger)*
	'then' script=Script
	'end'
;

EventTrigger:
	UpdateEventTrigger |
	GroupMemberUpdateEventTrigger |
	CommandEventTrigger |
	GroupMemberCommandEventTrigger |
	ChangedEventTrigger |
	GroupMemberChangedEventTrigger |
	EventEmittedTrigger |
	TimerTrigger |
	SystemTrigger |
	ThingStateUpdateEventTrigger |
	ThingStateChangedEventTrigger
;

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

GroupMemberCommandEventTrigger:
    'Member of' group=ItemName 'received command' (command=ValidCommand)?
;

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

GroupMemberUpdateEventTrigger:
    'Member of' group=ItemName 'received update' (state=ValidState)?
;

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

GroupMemberChangedEventTrigger:
	'Member of' group=ItemName 'changed' ('from' oldState=ValidState)? ('to' newState=ValidState)?
;		

EventEmittedTrigger:
	'Channel' channel=(STRING|ID) 'triggered' (trigger=ValidTrigger)?
;


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

SystemTrigger:
	SystemOnStartupTrigger |
	SystemOnShutdownTrigger
;

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

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

ThingStateUpdateEventTrigger:
    'Thing' thing=STRING 'received update' (state=ThingValidState)?
;

ThingStateChangedEventTrigger:
    'Thing' thing=STRING 'changed' ('from' oldState=ThingValidState)? ('to' newState=ThingValidState)?
;
	
ItemName :
	ID
;

ValidState:
    ValidStateId | ValidStateNumber | ValidStateString
;

ValidStateId:
    value=ID
;

ValidStateNumber:
    value=Number
;

ValidStateString:
    value=STRING
;

ValidCommand:
    ValidCommandId | ValidCommandNumber | ValidCommandString
;

ValidCommandId:
    value=ID
;

ValidCommandNumber:
    value=Number
;

ValidCommandString:
    value=STRING
;

ValidTrigger:
    ValidTriggerId | ValidTriggerNumber | ValidTriggerString
;

ValidTriggerId:
    value=ID
;

ValidTriggerNumber:
    value=Number
;

ValidTriggerString:
    value=STRING
;

ThingValidState:
    'UNINITIALIZED' |
    'INITIALIZING' |
    'UNKNOWN' |
    'ONLINE' |
    'OFFLINE' |
    'REMOVING' |
    'REMOVED'
;
