From e81739fb09835d04631b70d4486afc334e023624 Mon Sep 17 00:00:00 2001 From: Oliver Libutzki Date: Fri, 31 Jan 2014 13:54:52 +0100 Subject: [PATCH] Script editor and interpreter relies on JvmModelInferrer Signed-off-by: Oliver Libutzki --- .../model/rule/RulesRuntimeModule.java | 21 --- .../rule/jvmmodel/RulesJvmModelInferrer.xtend | 3 +- .../RuleExtensionClassNameProvider.java | 25 ---- .../rule/scoping/RulesFeatureScopes.java | 123 ----------------- .../rule/scoping/RulesScopeProvider.java | 129 ------------------ .../model/script/ui/ScriptUiModule.java | 5 +- .../model/script/ScriptRuntimeModule.java | 25 +--- .../interpreter/ScriptInterpreter.xtend | 41 ++++-- ...ScriptIdentifiableSimpleNameProvider.xtend | 16 --- .../jvmmodel/ScriptJvmModelInferrer.xtend | 59 +++++++- .../script/scoping/ActionClassLoader.java | 18 +++ .../ScriptExtensionClassNameProvider.java | 99 -------------- .../script/scoping/ScriptFeatureScopes.java | 72 ---------- .../ScriptImplicitlyImportedTypes.java | 53 ++++--- .../scoping/ScriptReentrantTypeResolver.java | 62 --------- .../script/scoping/ScriptScopeProvider.java | 91 ------------ 16 files changed, 138 insertions(+), 704 deletions(-) delete mode 100644 bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RuleExtensionClassNameProvider.java delete mode 100644 bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RulesFeatureScopes.java delete mode 100644 bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RulesScopeProvider.java delete mode 100644 bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/jvmmodel/ScriptIdentifiableSimpleNameProvider.xtend delete mode 100644 bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptExtensionClassNameProvider.java delete mode 100644 bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptFeatureScopes.java delete mode 100644 bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptReentrantTypeResolver.java delete mode 100644 bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptScopeProvider.java diff --git a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/RulesRuntimeModule.java b/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/RulesRuntimeModule.java index eb13cbf9..d0735825 100644 --- a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/RulesRuntimeModule.java +++ b/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/RulesRuntimeModule.java @@ -10,21 +10,15 @@ */ package org.eclipse.smarthome.model.rule; -import org.eclipse.smarthome.model.rule.scoping.RulesFeatureScopes; import org.eclipse.smarthome.model.rule.scoping.RulesImplicitlyImportedTypes; -import org.eclipse.smarthome.model.rule.scoping.RulesScopeProvider; import org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter; -import org.eclipse.smarthome.model.script.jvmmodel.ScriptIdentifiableSimpleNameProvider; import org.eclipse.smarthome.model.script.scoping.ActionClassLoader; import org.eclipse.smarthome.model.script.scoping.ActionClasspathBasedTypeScopeProvider; import org.eclipse.smarthome.model.script.scoping.ActionClasspathTypeProviderFactory; import org.eclipse.smarthome.model.script.scoping.StateAndCommandProvider; import org.eclipse.xtext.generator.IGenerator; import org.eclipse.xtext.generator.IGenerator.NullGenerator; -import org.eclipse.xtext.scoping.IScopeProvider; -import org.eclipse.xtext.xbase.featurecalls.IdentifiableSimpleNameProvider; import org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter; -import org.eclipse.xtext.xbase.scoping.batch.FeatureScopes; import org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedTypes; @@ -34,25 +28,14 @@ import org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedTypes; @SuppressWarnings("restriction") public class RulesRuntimeModule extends org.eclipse.smarthome.model.rule.AbstractRulesRuntimeModule { - - public Class bindIdentifiableSimpleNameProvider() { - return ScriptIdentifiableSimpleNameProvider.class; - } - public Class bindImplicitlyImportedTypes() { return RulesImplicitlyImportedTypes.class; } - public Class bindStateAndCommandProvider() { return StateAndCommandProvider.class; } - @Override - public Class bindIScopeProvider() { - return RulesScopeProvider.class; - } - /* we need this so that our pluggable actions can be resolved at design time */ @Override public Class bindIJvmTypeProvider$Factory() { @@ -71,10 +54,6 @@ public class RulesRuntimeModule extends org.eclipse.smarthome.model.rule.Abstrac return new ActionClassLoader(getClass().getClassLoader()); } - public Class bindFeatureScopes() { - return RulesFeatureScopes.class; - } - @Override public Class bindIGenerator() { return NullGenerator.class; diff --git a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/jvmmodel/RulesJvmModelInferrer.xtend b/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/jvmmodel/RulesJvmModelInferrer.xtend index 6158dd30..1fc5600e 100644 --- a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/jvmmodel/RulesJvmModelInferrer.xtend +++ b/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/jvmmodel/RulesJvmModelInferrer.xtend @@ -57,7 +57,8 @@ class RulesJvmModelInferrer extends ScriptJvmModelInferrer { def dispatch void infer(RuleModel ruleModel, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) { - acceptor.accept(ruleModel.toClass("test.TestClass")).initializeLater [ + val className = ruleModel.eResource.URI.lastSegment.split("\\.").head.toFirstUpper + "Rules" + acceptor.accept(ruleModel.toClass(className)).initializeLater [ members += ruleModel.variables.filter(XVariableDeclaration).map[ toField(name, type.cloneWithProxies) => [ static = true diff --git a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RuleExtensionClassNameProvider.java b/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RuleExtensionClassNameProvider.java deleted file mode 100644 index c9bba9f1..00000000 --- a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RuleExtensionClassNameProvider.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright (c) 2014 openHAB UG (haftungsbeschränkt) and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.eclipse.smarthome.model.rule.scoping; - -import org.eclipse.smarthome.model.script.scoping.ScriptExtensionClassNameProvider; - -import com.google.inject.Singleton; - -/** - * This class registers all statically available functions as well as the - * extensions for specific jvm types, which should only be available in rules, - * but not in scripts - * - * @author Kai Kreuzer - Initial contribution and API - * - */ -@Singleton -public class RuleExtensionClassNameProvider extends ScriptExtensionClassNameProvider { - -} diff --git a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RulesFeatureScopes.java b/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RulesFeatureScopes.java deleted file mode 100644 index e33abf00..00000000 --- a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RulesFeatureScopes.java +++ /dev/null @@ -1,123 +0,0 @@ -package org.eclipse.smarthome.model.rule.scoping; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EReference; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.smarthome.core.types.Command; -import org.eclipse.smarthome.core.types.State; -import org.eclipse.smarthome.model.rule.internal.engine.RuleContextHelper; -import org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger; -import org.eclipse.smarthome.model.rule.rules.CommandEventTrigger; -import org.eclipse.smarthome.model.rule.rules.EventTrigger; -import org.eclipse.smarthome.model.rule.rules.Rule; -import org.eclipse.smarthome.model.rule.rules.RuleModel; -import org.eclipse.smarthome.model.script.scoping.ScriptFeatureScopes; -import org.eclipse.xtext.EcoreUtil2; -import org.eclipse.xtext.common.types.JvmTypeReference; -import org.eclipse.xtext.common.types.util.TypeReferences; -import org.eclipse.xtext.naming.QualifiedName; -import org.eclipse.xtext.resource.IEObjectDescription; -import org.eclipse.xtext.resource.XtextResource; -import org.eclipse.xtext.scoping.IScope; -import org.eclipse.xtext.scoping.impl.MapBasedScope; -import org.eclipse.xtext.xbase.XExpression; -import org.eclipse.xtext.xbase.XVariableDeclaration; -import org.eclipse.xtext.xbase.XbaseFactory; -import org.eclipse.xtext.xbase.scoping.batch.IFeatureScopeSession; -import org.eclipse.xtext.xbase.scoping.featurecalls.LocalVarDescription; -import org.eclipse.xtext.xbase.typesystem.IResolvedTypes; - -import com.google.inject.Inject; - -public class RulesFeatureScopes extends ScriptFeatureScopes { - -// -// @Inject -// private TypeReferences typeReferences; -// -// -// @Override -// public IScope createSimpleFeatureCallScope(EObject context, -// EReference reference, IFeatureScopeSession session, -// IResolvedTypes resolvedTypes) { -// IScope parent = super.createSimpleFeatureCallScope(context, reference, session, -// resolvedTypes); -// Rule rule = EcoreUtil2.getContainerOfType(context,Rule.class); -// if(rule != null) { -// -// List descriptions = new ArrayList(); -// // descriptions.addAll(createVarFeatures(rule.eResource())); -// descriptions.addAll(createTriggerSpecificVars(rule)); -// return MapBasedScope.createScope(parent, descriptions); -// } else { -// return parent; -// } -// -// } -// -// private Collection createVarFeatures(Resource resource) { -// List descriptions = new ArrayList(); -// -// if(resource.getContents().size()>0 && resource.getContents().get(0) instanceof RuleModel) { -// RuleModel ruleModel = (RuleModel) resource.getContents().get(0); -// for(XExpression expr : ruleModel.getVariables()) { -// if (expr instanceof XVariableDeclaration) { -// XVariableDeclaration var = (XVariableDeclaration) expr; -// if(var.getName()!=null && var.getType()!=null) { -// descriptions.add(new LocalVarDescription(QualifiedName.create(var.getName()), var)); -// } -// } -// } -// } -// -// return descriptions; -// } -// -// private Collection createTriggerSpecificVars(Rule rule) { -// List descriptions = new ArrayList(); -// Resource varResource = new XtextResource(URI.createURI("event://specific.vars")); -// if(containsCommandTrigger(rule)) { -// JvmTypeReference commandTypeRef = typeReferences.getTypeForName(Command.class, rule); -// XVariableDeclaration varDecl = XbaseFactory.eINSTANCE.createXVariableDeclaration(); -// varDecl.setName(RuleContextHelper.VAR_RECEIVED_COMMAND); -// varDecl.setType(commandTypeRef); -// varDecl.setWriteable(false); -// varResource.getContents().add(varDecl); -// descriptions.add(new LocalVarDescription(QualifiedName.create(varDecl.getName()), varDecl)); -// } -// if(containsStateChangeTrigger(rule)) { -// JvmTypeReference stateTypeRef = typeReferences.getTypeForName(State.class, rule); -// XVariableDeclaration varDecl = XbaseFactory.eINSTANCE.createXVariableDeclaration(); -// varDecl.setName(RuleContextHelper.VAR_PREVIOUS_STATE); -// varDecl.setType(stateTypeRef); -// varDecl.setWriteable(false); -// varResource.getContents().add(varDecl); -// descriptions.add(new LocalVarDescription(QualifiedName.create(varDecl.getName()), varDecl)); -// } -// return descriptions; -// } -// -// private boolean containsCommandTrigger(Rule rule) { -// for(EventTrigger trigger : rule.getEventtrigger()) { -// if(trigger instanceof CommandEventTrigger) { -// return true; -// } -// } -// return false; -// } -// -// private boolean containsStateChangeTrigger(Rule rule) { -// for(EventTrigger trigger : rule.getEventtrigger()) { -// if(trigger instanceof ChangedEventTrigger) { -// return true; -// } -// } -// return false; -// } -// -} diff --git a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RulesScopeProvider.java b/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RulesScopeProvider.java deleted file mode 100644 index 1541e285..00000000 --- a/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/scoping/RulesScopeProvider.java +++ /dev/null @@ -1,129 +0,0 @@ -/** - * Copyright (c) 2014 openHAB UG (haftungsbeschr??nkt) and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -/* - * generated by Xtext - */ -package org.eclipse.smarthome.model.rule.scoping; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.smarthome.core.types.Command; -import org.eclipse.smarthome.core.types.State; -import org.eclipse.smarthome.model.rule.internal.engine.RuleContextHelper; -import org.eclipse.smarthome.model.script.scoping.ScriptScopeProvider; -import org.eclipse.xtext.common.types.JvmTypeReference; -import org.eclipse.xtext.common.types.util.TypeReferences; -import org.eclipse.xtext.naming.QualifiedName; -import org.eclipse.xtext.resource.IEObjectDescription; -import org.eclipse.xtext.resource.XtextResource; -import org.eclipse.xtext.scoping.IScope; -import org.eclipse.xtext.scoping.impl.MapBasedScope; -import org.eclipse.xtext.xbase.XExpression; -import org.eclipse.xtext.xbase.XVariableDeclaration; -import org.eclipse.xtext.xbase.XbaseFactory; -import org.eclipse.xtext.xbase.scoping.LocalVariableScopeContext; -import org.eclipse.xtext.xbase.scoping.featurecalls.LocalVarDescription; -import org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger; -import org.eclipse.smarthome.model.rule.rules.CommandEventTrigger; -import org.eclipse.smarthome.model.rule.rules.EventTrigger; -import org.eclipse.smarthome.model.rule.rules.Rule; -import org.eclipse.smarthome.model.rule.rules.RuleModel; - -import com.google.inject.Inject; - - -/** - * This scope provider adds all things to the scope which are specific to rules. - * - * @author Kai Kreuzer - Initial contribution and API - * - */ -@SuppressWarnings("restriction") -public class RulesScopeProvider extends ScriptScopeProvider { -// -// @Inject -// private TypeReferences typeReferences; -// -// @Override -// protected IScope createLocalVarScope(IScope parentScope, -// LocalVariableScopeContext scopeContext) { -// if(scopeContext.getContext() instanceof Rule) { -// IScope parent = super.createLocalVarScope(parentScope, scopeContext); -// List descriptions = new ArrayList(); -// descriptions.addAll(createVarFeatures(scopeContext.getContext().eResource())); -// descriptions.addAll(createTriggerSpecificVars((Rule) scopeContext.getContext())); -// return MapBasedScope.createScope(parent, descriptions); -// } else { -// return super.createLocalVarScope(parentScope, scopeContext); -// } -// } -// -// private Collection createVarFeatures(Resource resource) { -// List descriptions = new ArrayList(); -// -// if(resource.getContents().size()>0 && resource.getContents().get(0) instanceof RuleModel) { -// RuleModel ruleModel = (RuleModel) resource.getContents().get(0); -// for(XExpression expr : ruleModel.getVariables()) { -// if (expr instanceof XVariableDeclaration) { -// XVariableDeclaration var = (XVariableDeclaration) expr; -// if(var.getName()!=null && var.getType()!=null) { -// descriptions.add(createLocalVarDescription(var)); -// } -// } -// } -// } -// -// return descriptions; -// } -// -// private Collection createTriggerSpecificVars(Rule rule) { -// List descriptions = new ArrayList(); -// Resource varResource = new XtextResource(URI.createURI("event://specific.vars")); -// if(containsCommandTrigger(rule)) { -// JvmTypeReference commandTypeRef = typeReferences.getTypeForName(Command.class, rule); -// XVariableDeclaration varDecl = XbaseFactory.eINSTANCE.createXVariableDeclaration(); -// varDecl.setName(RuleContextHelper.VAR_RECEIVED_COMMAND); -// varDecl.setType(commandTypeRef); -// varDecl.setWriteable(false); -// varResource.getContents().add(varDecl); -// descriptions.add(new LocalVarDescription(QualifiedName.create(varDecl.getName()), varDecl)); -// } -// if(containsStateChangeTrigger(rule)) { -// JvmTypeReference stateTypeRef = typeReferences.getTypeForName(State.class, rule); -// XVariableDeclaration varDecl = XbaseFactory.eINSTANCE.createXVariableDeclaration(); -// varDecl.setName(RuleContextHelper.VAR_PREVIOUS_STATE); -// varDecl.setType(stateTypeRef); -// varDecl.setWriteable(false); -// varResource.getContents().add(varDecl); -// descriptions.add(new LocalVarDescription(QualifiedName.create(varDecl.getName()), varDecl)); -// } -// return descriptions; -// } -// -// private boolean containsCommandTrigger(Rule rule) { -// for(EventTrigger trigger : rule.getEventtrigger()) { -// if(trigger instanceof CommandEventTrigger) { -// return true; -// } -// } -// return false; -// } -// -// private boolean containsStateChangeTrigger(Rule rule) { -// for(EventTrigger trigger : rule.getEventtrigger()) { -// if(trigger instanceof ChangedEventTrigger) { -// return true; -// } -// } -// return false; -// } -} diff --git a/bundles/model/org.eclipse.smarthome.model.script.ui/src/org/eclipse/smarthome/model/script/ui/ScriptUiModule.java b/bundles/model/org.eclipse.smarthome.model.script.ui/src/org/eclipse/smarthome/model/script/ui/ScriptUiModule.java index f9b899a2..6d10a914 100644 --- a/bundles/model/org.eclipse.smarthome.model.script.ui/src/org/eclipse/smarthome/model/script/ui/ScriptUiModule.java +++ b/bundles/model/org.eclipse.smarthome.model.script.ui/src/org/eclipse/smarthome/model/script/ui/ScriptUiModule.java @@ -10,10 +10,10 @@ */ package org.eclipse.smarthome.model.script.ui; +import org.eclipse.smarthome.model.script.scoping.ActionClasspathBasedTypeScopeProvider; import org.eclipse.smarthome.model.script.scoping.ActionClasspathTypeProviderFactory; import org.eclipse.smarthome.model.script.ui.contentassist.ActionEObjectHoverProvider; import org.eclipse.ui.plugin.AbstractUIPlugin; -import org.eclipse.xtext.common.types.xtext.ClasspathBasedTypeScopeProvider; import org.eclipse.xtext.ui.editor.hover.IEObjectHoverProvider; /** @@ -25,13 +25,12 @@ public class ScriptUiModule extends org.eclipse.smarthome.model.script.ui.Abstra super(plugin); } - @Override public Class bindIJvmTypeProvider$Factory() { return ActionClasspathTypeProviderFactory.class; } public Class bindAbstractTypeScopeProvider() { - return ClasspathBasedTypeScopeProvider.class; + return ActionClasspathBasedTypeScopeProvider.class; } public Class bindIEObjectHoverProvider() { diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/ScriptRuntimeModule.java b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/ScriptRuntimeModule.java index f8ce3141..313cbbd5 100644 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/ScriptRuntimeModule.java +++ b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/ScriptRuntimeModule.java @@ -13,19 +13,13 @@ package org.eclipse.smarthome.model.script; import org.eclipse.smarthome.core.scriptengine.Script; import org.eclipse.smarthome.model.script.internal.engine.ScriptImpl; import org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter; -import org.eclipse.smarthome.model.script.jvmmodel.ScriptIdentifiableSimpleNameProvider; import org.eclipse.smarthome.model.script.scoping.ActionClassLoader; import org.eclipse.smarthome.model.script.scoping.ActionClasspathBasedTypeScopeProvider; import org.eclipse.smarthome.model.script.scoping.ActionClasspathTypeProviderFactory; -import org.eclipse.smarthome.model.script.scoping.ScriptFeatureScopes; import org.eclipse.smarthome.model.script.scoping.ScriptImplicitlyImportedTypes; -import org.eclipse.smarthome.model.script.scoping.ScriptScopeProvider; -import org.eclipse.smarthome.model.script.scoping.StateAndCommandProvider; import org.eclipse.xtext.generator.IGenerator; import org.eclipse.xtext.generator.IGenerator.NullGenerator; -import org.eclipse.xtext.xbase.featurecalls.IdentifiableSimpleNameProvider; import org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter; -import org.eclipse.xtext.xbase.scoping.batch.FeatureScopes; import org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedTypes; /** @@ -39,10 +33,6 @@ public class ScriptRuntimeModule extends org.eclipse.smarthome.model.script.Abst return ScriptImplicitlyImportedTypes.class; } - public Class bindIdentifiableSimpleNameProvider() { - return ScriptIdentifiableSimpleNameProvider.class; - } - public Class bindScript() { return ScriptImpl.class; } @@ -51,15 +41,6 @@ public class ScriptRuntimeModule extends org.eclipse.smarthome.model.script.Abst return ScriptInterpreter.class; } - public Class bindStateAndCommandProvider() { - return StateAndCommandProvider.class; - } - - @Override - public Class bindIScopeProvider() { - return ScriptScopeProvider.class; - } - /* we need this so that our pluggable actions can be resolved at design time */ @Override public Class bindIJvmTypeProvider$Factory() { @@ -78,13 +59,9 @@ public class ScriptRuntimeModule extends org.eclipse.smarthome.model.script.Abst return new ActionClassLoader(getClass().getClassLoader()); } - public Class bindFeatureScopes() { - return ScriptFeatureScopes.class; - } - @Override public Class bindIGenerator() { return NullGenerator.class; } - + } diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/interpreter/ScriptInterpreter.xtend b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/interpreter/ScriptInterpreter.xtend index d46f0d10..b81acbf9 100644 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/interpreter/ScriptInterpreter.xtend +++ b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/interpreter/ScriptInterpreter.xtend @@ -17,6 +17,13 @@ import org.eclipse.smarthome.model.script.scoping.StateAndCommandProvider import org.eclipse.xtext.common.types.JvmField import org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations +import org.eclipse.xtext.xbase.XAssignment +import org.eclipse.xtext.xbase.interpreter.IEvaluationContext +import org.eclipse.xtext.util.CancelIndicator +import org.eclipse.xtext.xbase.interpreter.impl.EvaluationException +import org.eclipse.xtext.naming.QualifiedName +import org.eclipse.xtext.xbase.XAbstractFeatureCall +import org.eclipse.xtext.xbase.XVariableDeclaration /** * The script interpreter handles the openHAB specific script components, which are not known @@ -38,20 +45,26 @@ public class ScriptInterpreter extends XbaseInterpreter { @Inject extension IJvmModelAssociations - override protected Object featureCallField(JvmField jvmField, Object receiver) { + override protected _invokeFeature(JvmField jvmField, XAbstractFeatureCall featureCall, Object receiver, IEvaluationContext context, CancelIndicator indicator) { // Check if the JvmField is inferred val sourceElement = jvmField.sourceElements.head if (sourceElement != null) { - // Looks like we have an item field - for(Type type : stateAndCommandProvider.getAllTypes()) { - if (type.toString == jvmField.simpleName) { - return type + switch sourceElement { + XVariableDeclaration : return context.getValue(QualifiedName.create(jvmField.simpleName)) + default: { + // Looks like we have an item field + for(Type type : stateAndCommandProvider.getAllTypes()) { + if (type.toString == jvmField.simpleName) { + return type + } + } + return getItem(jvmField.simpleName) } } - return getItem(jvmField.simpleName) } else { - super.featureCallField(jvmField, receiver) - } + super._invokeFeature(jvmField, featureCall, receiver, context, indicator) + } + } @@ -73,4 +86,16 @@ public class ScriptInterpreter extends XbaseInterpreter { return super.eq(a, b); } } + + override protected _assigneValueTo(JvmField jvmField, XAssignment assignment, Object value, IEvaluationContext context, CancelIndicator indicator) { + // Check if the JvmField is inferred + val sourceElement = jvmField.sourceElements.head + if (sourceElement != null) { + context.assignValue(QualifiedName.create(jvmField.simpleName), value) + value + } else { + super._assigneValueTo(jvmField, assignment, value, context, indicator) + } + } + } diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/jvmmodel/ScriptIdentifiableSimpleNameProvider.xtend b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/jvmmodel/ScriptIdentifiableSimpleNameProvider.xtend deleted file mode 100644 index c548a1f5..00000000 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/jvmmodel/ScriptIdentifiableSimpleNameProvider.xtend +++ /dev/null @@ -1,16 +0,0 @@ -package org.eclipse.smarthome.model.script.jvmmodel - -import org.eclipse.xtext.xbase.featurecalls.IdentifiableSimpleNameProvider -import org.eclipse.xtext.common.types.JvmType -import org.eclipse.xtext.common.types.JvmIdentifiableElement - -class ScriptIdentifiableSimpleNameProvider extends IdentifiableSimpleNameProvider { - - def dispatch getSimpleName(JvmType element) { - return "this"; - } - - def dispatch getSimpleName(JvmIdentifiableElement element) { - return super.getSimpleName(element); - } -} diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/jvmmodel/ScriptJvmModelInferrer.xtend b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/jvmmodel/ScriptJvmModelInferrer.xtend index 67d1eee8..407cc154 100644 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/jvmmodel/ScriptJvmModelInferrer.xtend +++ b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/jvmmodel/ScriptJvmModelInferrer.xtend @@ -1,9 +1,18 @@ package org.eclipse.smarthome.model.script.jvmmodel import com.google.inject.Inject +import java.util.Set +import org.eclipse.smarthome.core.types.Command +import org.eclipse.smarthome.core.types.State +import org.eclipse.smarthome.model.script.engine.ItemRegistryProvider +import org.eclipse.smarthome.model.script.scoping.StateAndCommandProvider +import org.eclipse.smarthome.model.script.script.Script import org.eclipse.xtext.naming.IQualifiedNameProvider import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer +import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder +import org.slf4j.Logger +import org.slf4j.LoggerFactory /** *

Infers a JVM model from the source model.

@@ -13,11 +22,20 @@ import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder */ class ScriptJvmModelInferrer extends AbstractModelInferrer { + static private final Logger logger = LoggerFactory.getLogger(ScriptJvmModelInferrer) + /** * conveninence API to build and initialize JvmTypes and their members. */ @Inject extension JvmTypesBuilder - @Inject extension IQualifiedNameProvider + @Inject extension IQualifiedNameProvider + + + @Inject + ItemRegistryProvider itemRegistryProvider + + @Inject + StateAndCommandProvider stateAndCommandProvider /** * Is called for each instance of the first argument's type contained in a resource. @@ -28,6 +46,41 @@ class ScriptJvmModelInferrer extends AbstractModelInferrer { * @param isPreLinkingPhase - whether the method is called in a pre linking phase, i.e. when the global index isn't fully updated. You * must not rely on linking using the index if iPrelinkingPhase is true */ -// def dispatch infer(DecimalLiteral literal, IAcceptor acceptor, boolean prelinkingPhase) { -// } + def dispatch void infer(Script script, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) { + val className = script.eResource.URI.lastSegment.split("\\.").head.toFirstUpper + "Script" + acceptor.accept(script.toClass(className)).initializeLater [ + + + val Set fieldNames = newHashSet() + + val types = stateAndCommandProvider.allTypes + types.forEach [ type | + val name = type.toString + if (fieldNames.add(name)) { + members += script.toField(name, script.newTypeRef(type.class)) [ + static = true + ] + } else { + logger.warn("Duplicate field: '{}'. Ignoring '{}'.", name, type.class.name) + } + ] + + val itemRegistry = itemRegistryProvider.get + itemRegistry.items.forEach[ item | + val name = item.name + if (fieldNames.add(name)) { + members += script.toField(item.name, script.newTypeRef(item.class)) [ + static = true + ] + } else { + logger.warn("Duplicate field: '{}'. Ignoring '{}'.", item.name, item.class.name) + } + ] + + members += script.toMethod("_script", null) [ + static = true + body = script + ] + ] + } } diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ActionClassLoader.java b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ActionClassLoader.java index c01a07a0..0f63e4fb 100644 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ActionClassLoader.java +++ b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ActionClassLoader.java @@ -7,6 +7,9 @@ */ package org.eclipse.smarthome.model.script.scoping; +import java.net.MalformedURLException; +import java.net.URL; + import org.eclipse.smarthome.core.scriptengine.action.ActionService; import org.eclipse.smarthome.model.script.internal.ScriptActivator; @@ -42,4 +45,19 @@ final public class ActionClassLoader extends ClassLoader { } throw new ClassNotFoundException(); } + + @Override + protected URL findResource(String name) { + Object[] services = ScriptActivator.actionServiceTracker.getServices(); + if(services!=null) { + for(Object service : services) { + ActionService actionService = (ActionService) service; + URL url = actionService.getActionClass().getClassLoader().getResource(name); + if (url != null) { + return url; + } + } + } + return null; + } } \ No newline at end of file diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptExtensionClassNameProvider.java b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptExtensionClassNameProvider.java deleted file mode 100644 index 564c609e..00000000 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptExtensionClassNameProvider.java +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Copyright (c) 2014 openHAB UG (haftungsbeschränkt) and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.eclipse.smarthome.model.script.scoping; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.net.URLEncoder; -import java.util.Collection; - -import org.apache.commons.lang.StringUtils; -import org.eclipse.smarthome.core.items.Item; -import org.eclipse.smarthome.core.persistence.extensions.PersistenceExtensions; -import org.eclipse.smarthome.core.scriptengine.action.ActionService; -import org.eclipse.smarthome.core.types.Type; -import org.eclipse.smarthome.model.script.actions.BusEvent; -import org.eclipse.smarthome.model.script.actions.LogAction; -import org.eclipse.smarthome.model.script.actions.ScriptExecution; -import org.eclipse.smarthome.model.script.internal.ScriptActivator; -import org.eclipse.smarthome.model.script.lib.NumberExtensions; -import org.eclipse.xtext.xbase.scoping.featurecalls.StaticImplicitMethodsFeatureForTypeProvider.ExtensionClassNameProvider; -import org.joda.time.DateMidnight; -import org.joda.time.DateTime; - -import com.google.common.collect.Multimap; -import com.google.inject.Singleton; - -/** - * This class registers all statically available functions as well as the - * extensions for specific jvm types. - * - * @author Kai Kreuzer - Initial contribution and API - * - */ -@SuppressWarnings("restriction") -@Singleton -public class ScriptExtensionClassNameProvider extends ExtensionClassNameProvider { - - private int trackingCount = -1; - - @Override - protected Collection getLiteralClassNames() { - int currentTrackingCount = ScriptActivator.actionServiceTracker.getTrackingCount(); - - // if something has changed about the tracked services, recompute the list - if(trackingCount != currentTrackingCount) { - trackingCount = currentTrackingCount; - return computeLiteralClassNames(); - } else { - return super.getLiteralClassNames(); - } - } - - @Override - protected Collection computeLiteralClassNames() { - Collection extensions = super.computeLiteralClassNames(); - - // add all actions that are contributed as OSGi services - Object[] services = ScriptActivator.actionServiceTracker.getServices(); - if(services!=null) { - for(Object service : services) { - ActionService actionService = (ActionService) service; - extensions.add(actionService.getActionClassName()); - } - } - - extensions.add(BusEvent.class.getCanonicalName()); - extensions.add(ScriptExecution.class.getCanonicalName()); - extensions.add(LogAction.class.getCanonicalName()); - - // jodatime static functions - extensions.add(DateTime.class.getCanonicalName()); - extensions.add(DateMidnight.class.getCanonicalName()); - return extensions; - } - - @Override - protected Multimap, Class> simpleComputeExtensionClasses() { - Multimap, Class> result = super.simpleComputeExtensionClasses(); - result.removeAll(Comparable.class); - result.removeAll(Double.class); - result.removeAll(Integer.class); - result.removeAll(BigInteger.class); - result.removeAll(BigDecimal.class); - result.removeAll(double.class); - result.put(Number.class, NumberExtensions.class); - result.put(Type.class, NumberExtensions.class); - result.put(Comparable.class, NumberExtensions.class); - result.put(String.class, StringUtils.class); - result.put(String.class, URLEncoder.class); - result.put(Item.class, PersistenceExtensions.class); - result.put(Item.class, BusEvent.class); - return result; - } -} diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptFeatureScopes.java b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptFeatureScopes.java deleted file mode 100644 index cb57369f..00000000 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptFeatureScopes.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.eclipse.smarthome.model.script.scoping; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EReference; -import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.smarthome.core.items.Item; -import org.eclipse.smarthome.core.items.ItemRegistry; -import org.eclipse.smarthome.core.types.Type; -import org.eclipse.smarthome.model.script.engine.ItemRegistryProvider; -import org.eclipse.xtext.common.types.access.IJvmTypeProvider; -import org.eclipse.xtext.resource.EObjectDescription; -import org.eclipse.xtext.resource.IEObjectDescription; -import org.eclipse.xtext.scoping.IScope; -import org.eclipse.xtext.scoping.impl.MapBasedScope; -import org.eclipse.xtext.xbase.scoping.batch.FeatureScopes; -import org.eclipse.xtext.xbase.scoping.batch.IFeatureScopeSession; -import org.eclipse.xtext.xbase.typesystem.IResolvedTypes; - -import com.google.inject.Inject; - -public class ScriptFeatureScopes extends FeatureScopes { - - - @Inject - private IJvmTypeProvider.Factory typeProviderFactory; - - @Inject - private ItemRegistryProvider itemRegistryProvider; - - @Inject - private StateAndCommandProvider stateAndCommandProvider; - -// @Override -// public IScope createSimpleFeatureCallScope(EObject context, -// EReference reference, IFeatureScopeSession session, -// IResolvedTypes resolvedTypes) { -// IScope parent = super.createSimpleFeatureCallScope(context, reference, session, -// resolvedTypes); -// List descriptions = new ArrayList(); -// descriptions.addAll(createItemFeatures(context.eResource().getResourceSet())); -// descriptions.addAll(createTypeFeatures(context.eResource().getResourceSet())); -// -// return MapBasedScope.createScope(parent, descriptions); -// } -// -// private Collection createTypeFeatures(ResourceSet rs) { -// -// List descriptions = new ArrayList(); -// IJvmTypeProvider provider = typeProviderFactory.findOrCreateTypeProvider(rs); -// for(Type type : stateAndCommandProvider.getAllTypes()) { -// descriptions.add(EObjectDescription.create(type.toString(), provider.findTypeByName(type.getClass().getCanonicalName()))); -// } -// -// return descriptions; -// } -// -// private List createItemFeatures(ResourceSet rs) { -// IJvmTypeProvider provider = typeProviderFactory.findOrCreateTypeProvider(rs); -// List descriptions = new ArrayList(); -// ItemRegistry itemRegistry = itemRegistryProvider.get(); -// if(itemRegistry!=null) { -// for(Item item : itemRegistry.getItems()) { -// descriptions.add(EObjectDescription.create(item.getName(), provider.findTypeByName(item.getClass().getCanonicalName()))); -// } -// } -// return descriptions; -// } -} diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptImplicitlyImportedTypes.java b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptImplicitlyImportedTypes.java index eaba9bdc..d9bc4dda 100644 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptImplicitlyImportedTypes.java +++ b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptImplicitlyImportedTypes.java @@ -41,10 +41,7 @@ import com.google.inject.Singleton; @Singleton public class ScriptImplicitlyImportedTypes extends ImplicitlyImportedTypes { - @Inject - private TypeReferences typeReferences; - - private Collection staticImportClassNames = null; + private List> actionClasses = null; private int trackingCount = -1; @@ -65,24 +62,24 @@ public class ScriptImplicitlyImportedTypes extends ImplicitlyImportedTypes { return result; } - @Override - public List getStaticImportClasses(Resource context) { - List result = super.getStaticImportClasses(context); - - Collection classNames = getStaticImportClassNames(); - result.addAll(getClassNameTypes(classNames, context)); - return result; - } - - protected Collection getClassNameTypes(Collection classNames, Resource context) { - List result = Lists.newArrayListWithCapacity(classNames.size()); - for(String className: classNames) { - JvmType type = typeReferences.findDeclaredType(className, context); - if (type != null) - result.add(type); - } - return result; - } +// @Override +// public List getStaticImportClasses(Resource context) { +// List result = super.getStaticImportClasses(context); +// +// List> actionClasses = getActionClasses(); +// result.addAll(getTypes(actionClasses, context)); +// return result; +// } +// +// protected Collection getClassNameTypes(Collection classNames, Resource context) { +// List result = Lists.newArrayListWithCapacity(classNames.size()); +// for(String className: classNames) { +// JvmType type = typeReferences.findDeclaredType(className, context); +// if (type != null) +// result.add(type); +// } +// return result; +// } @Override protected List> getStaticImportClasses() { @@ -94,27 +91,29 @@ public class ScriptImplicitlyImportedTypes extends ImplicitlyImportedTypes { // jodatime static functions result.add(DateTime.class); result.add(DateMidnight.class); + + result.addAll(getActionClasses()); return result; } - protected Collection getStaticImportClassNames() { + protected List> getActionClasses() { int currentTrackingCount = ScriptActivator.actionServiceTracker.getTrackingCount(); // if something has changed about the tracked services, recompute the list if(trackingCount != currentTrackingCount) { trackingCount = currentTrackingCount; - Collection extensions = new ArrayList(); + List> privateActionClasses = new ArrayList>(); // add all actions that are contributed as OSGi services Object[] services = ScriptActivator.actionServiceTracker.getServices(); if(services!=null) { for(Object service : services) { ActionService actionService = (ActionService) service; - extensions.add(actionService.getActionClassName()); + privateActionClasses.add(actionService.getActionClass()); } } - staticImportClassNames=extensions; + this.actionClasses=privateActionClasses; } - return staticImportClassNames; + return this.actionClasses; } } \ No newline at end of file diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptReentrantTypeResolver.java b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptReentrantTypeResolver.java deleted file mode 100644 index 5bdf9cd4..00000000 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptReentrantTypeResolver.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.eclipse.smarthome.model.script.scoping; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.jdt.annotation.Nullable; -import org.eclipse.smarthome.core.items.Item; -import org.eclipse.smarthome.core.items.ItemRegistry; -import org.eclipse.smarthome.model.script.engine.ItemRegistryProvider; -import org.eclipse.xtext.common.types.JvmDeclaredType; -import org.eclipse.xtext.common.types.JvmIdentifiableElement; -import org.eclipse.xtext.common.types.JvmTypeReference; -import org.eclipse.xtext.common.types.access.IJvmTypeProvider; -import org.eclipse.xtext.naming.QualifiedName; -import org.eclipse.xtext.resource.EObjectDescription; -import org.eclipse.xtext.resource.IEObjectDescription; -import org.eclipse.xtext.xbase.scoping.batch.IFeatureNames; -import org.eclipse.xtext.xbase.scoping.batch.IFeatureScopeSession; -import org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareReentrantTypeResolver; -import org.eclipse.xtext.xbase.typesystem.internal.ResolvedTypes; -import org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner; - -import com.google.common.collect.ImmutableMap; -import com.google.inject.Inject; - -public class ScriptReentrantTypeResolver extends - LogicalContainerAwareReentrantTypeResolver { - - @Inject - private IJvmTypeProvider.Factory typeProviderFactory; - - @Inject - private ItemRegistryProvider itemRegistryProvider; - - @Override - protected Map prepare( - ResolvedTypes resolvedTypes, - IFeatureScopeSession featureScopeSession) { - addItems(featureScopeSession, resolvedTypes.getReferenceOwner(), (JvmDeclaredType)getRootJvmType()); - return super.prepare(resolvedTypes, featureScopeSession); - } - - - protected IFeatureScopeSession addItems(IFeatureScopeSession session, ITypeReferenceOwner owner, JvmDeclaredType thisType) { - IFeatureScopeSession childSession = null; - IJvmTypeProvider provider = typeProviderFactory.findOrCreateTypeProvider(thisType.eResource().getResourceSet()); - List descriptions = new ArrayList(); - ItemRegistry itemRegistry = itemRegistryProvider.get(); - if(itemRegistry!=null) { - ImmutableMap.Builder builder = ImmutableMap.builder(); - for(Item item : itemRegistry.getItems()) { - builder.put(QualifiedName.create(item.getName()), provider.findTypeByName(item.getClass().getCanonicalName())); - } - childSession = session.addLocalElements(builder.build(), owner); - } - childSession = addThisTypeToStaticScope(childSession, thisType); - return childSession; - } - -} diff --git a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptScopeProvider.java b/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptScopeProvider.java deleted file mode 100644 index 91a45564..00000000 --- a/bundles/model/org.eclipse.smarthome.model.script/src/org/eclipse/smarthome/model/script/scoping/ScriptScopeProvider.java +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Copyright (c) 2014 openHAB UG (haftungsbeschränkt) and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.eclipse.smarthome.model.script.scoping; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EReference; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.smarthome.core.items.Item; -import org.eclipse.smarthome.core.items.ItemRegistry; -import org.eclipse.smarthome.core.types.Type; -import org.eclipse.smarthome.model.script.engine.ItemRegistryProvider; -import org.eclipse.xtext.common.types.TypesFactory; -import org.eclipse.xtext.common.types.access.IJvmTypeProvider; -import org.eclipse.xtext.resource.EObjectDescription; -import org.eclipse.xtext.resource.IEObjectDescription; -import org.eclipse.xtext.scoping.IScope; -import org.eclipse.xtext.scoping.impl.MapBasedScope; -import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations; -import org.eclipse.xtext.xbase.scoping.XbaseScopeProvider; - -import com.google.inject.Inject; - -/** - * This scope provider adds all items, states and commands to the scope, so that - * these are always available in the scripts. - * - * @author Kai Kreuzer - Initial contribution and API - * - */ -@SuppressWarnings("all") -public class ScriptScopeProvider extends XbaseScopeProvider { - - @Inject - private IJvmTypeProvider.Factory typeProviderFactory; - - @Inject - private ItemRegistryProvider itemRegistryProvider; - - @Inject - private StateAndCommandProvider stateAndCommandProvider; - - public ScriptScopeProvider() { - } - - @Override - public IScope createSimpleFeatureCallScope(EObject context, - EReference reference, Resource resource, - boolean includeCurrentBlock, int idx) { - IScope parent = super.createSimpleFeatureCallScope(context, reference, resource, - includeCurrentBlock, idx); - List descriptions = new ArrayList(); - descriptions.addAll(createItemFeatures(resource.getResourceSet())); - descriptions.addAll(createTypeFeatures(resource.getResourceSet())); - - return MapBasedScope.createScope(parent, descriptions); - } - - private Collection createTypeFeatures(ResourceSet rs) { - - List descriptions = new ArrayList(); - IJvmTypeProvider provider = typeProviderFactory.findOrCreateTypeProvider(rs); - for(Type type : stateAndCommandProvider.getAllTypes()) { - descriptions.add(EObjectDescription.create(type.toString(), provider.findTypeByName(type.getClass().getCanonicalName()))); - } - - return descriptions; - } - - private List createItemFeatures(ResourceSet rs) { - IJvmTypeProvider provider = typeProviderFactory.findOrCreateTypeProvider(rs); - List descriptions = new ArrayList(); - ItemRegistry itemRegistry = itemRegistryProvider.get(); - if(itemRegistry!=null) { - for(Item item : itemRegistry.getItems()) { - descriptions.add(EObjectDescription.create(item.getName(), provider.findTypeByName(item.getClass().getCanonicalName()))); - } - } - return descriptions; - } - -} \ No newline at end of file