本文整理汇总了Java中groovy.lang.GroovyCodeSource类的典型用法代码示例。如果您正苦于以下问题:Java GroovyCodeSource类的具体用法?Java GroovyCodeSource怎么用?Java GroovyCodeSource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GroovyCodeSource类属于groovy.lang包,在下文中一共展示了GroovyCodeSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Test
public void test() {
ActionDb actionDb = new ActionDslLoader(new GroovyCodeSource(getClass().getResource("/actions.groovy"))).load(network);
assertEquals(2, actionDb.getContingencies().size());
Contingency contingency = actionDb.getContingency("contingency1");
ContingencyElement element = contingency.getElements().iterator().next();
assertEquals("NHV1_NHV2_1", element.getId());
contingency = actionDb.getContingency("contingency2");
element = contingency.getElements().iterator().next();
assertEquals("GEN", element.getId());
assertEquals(1, actionDb.getRules().size());
Rule rule = actionDb.getRules().iterator().next();
assertEquals("rule", rule.getId());
assertEquals("rule description", rule.getDescription());
assertTrue(rule.getActions().contains("action"));
assertEquals(2, rule.getLife());
Action action = actionDb.getAction("action");
assertEquals("action", action.getId());
assertEquals("action description", action.getDescription());
assertEquals(0, action.getTasks().size());
}
示例2: setUp
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
network = createNetwork();
ComputationManager computationManager = Mockito.mock(ComputationManager.class);
LoadFlow loadFlow = Mockito.mock(LoadFlow.class);
LoadFlowFactory loadFlowFactory = Mockito.mock(LoadFlowFactory.class);
Mockito.when(loadFlowFactory.create(Mockito.any(Network.class), Mockito.any(ComputationManager.class), Mockito.anyInt()))
.thenReturn(loadFlow);
LoadFlowResult loadFlowResult = Mockito.mock(LoadFlowResult.class);
Mockito.when(loadFlowResult.isOk())
.thenReturn(true);
Mockito.when(loadFlow.getName()).thenReturn("load flow mock");
Mockito.when(loadFlow.run())
.thenReturn(loadFlowResult);
LoadFlowActionSimulatorObserver observer = createObserver();
GroovyCodeSource src = new GroovyCodeSource(new InputStreamReader(getClass().getResourceAsStream(getDslFile())), "test", GroovyShell.DEFAULT_CODE_BASE);
actionDb = new ActionDslLoader(src).load(network);
engine = new LoadFlowActionSimulator(network, computationManager, new LoadFlowActionSimulatorConfig(LoadFlowFactory.class, 3, false), observer) {
@Override
protected LoadFlowFactory newLoadFlowFactory() {
return loadFlowFactory;
}
};
}
示例3: run
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
/**
* Standalone execution for Designer and Gradle.
*/
public void run() {
startExecution();
CompilerConfiguration compilerConfig = new CompilerConfiguration(System.getProperties());
compilerConfig.setScriptBaseClass(TestCaseScript.class.getName());
Binding binding = new Binding();
binding.setVariable("testCaseRun", this);
ClassLoader classLoader = this.getClass().getClassLoader();
GroovyShell shell = new GroovyShell(classLoader, binding, compilerConfig);
shell.setProperty("out", getLog());
setupContextClassLoader(shell);
try {
shell.run(new GroovyCodeSource(getTestCase().file()), new String[0]);
finishExecution(null);
}
catch (IOException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
示例4: prepareGroovyCodeSource
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Override
protected GroovyCodeSource prepareGroovyCodeSource(String dsl) {
ScriptApproval.get()
.configuring(
dsl,
GroovyLanguage.get(),
ApprovalContext.create().
withCurrentUser()
.withItem(source)
);
try {
ScriptApproval.get().using(dsl, GroovyLanguage.get());
} catch (RejectedAccessException e) {
throw ScriptApproval.get().accessRejected(e, ApprovalContext.create().withItem(source));
}
return super.prepareGroovyCodeSource(dsl);
}
示例5: run
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Override
public Object run(String dsl, Binding binding) {
CompilerConfiguration compilerConfiguration = prepareCompilerConfiguration();
ClassLoader classLoader = prepareClassLoader(AbstractDSLLauncher.class.getClassLoader());
GroovyCodeSource groovyCodeSource = prepareGroovyCodeSource(dsl);
// Groovy shell
GroovyShell shell = new GroovyShell(
classLoader,
new Binding(),
compilerConfiguration
);
// Groovy script
Script groovyScript = shell.parse(groovyCodeSource);
// Binding
groovyScript.setBinding(binding);
// Runs the script
return run(groovyScript);
}
示例6: doParseClass
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
private Class<?> doParseClass(GroovyCodeSource codeSource) {
// local is kept as hard reference to avoid garbage collection
ThreadLocal<LocalData> localTh = getLocalData();
LocalData localData = new LocalData();
localTh.set(localData);
StringSetMap cache = localData.dependencyCache;
Class<?> answer = null;
try {
updateLocalDependencyCache(codeSource, localData);
answer = super.parseClass(codeSource, false);
updateScriptCache(localData);
} finally {
cache.clear();
localTh.remove();
}
return answer;
}
示例7: updateLocalDependencyCache
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
private void updateLocalDependencyCache(GroovyCodeSource codeSource, LocalData localData) {
// we put the old dependencies into local cache so createCompilationUnit
// can pick it up. We put that entry under the name "."
ScriptCacheEntry origEntry = scriptCache.get(codeSource.getName());
Set<String> origDep = null;
if (origEntry != null) origDep = origEntry.dependencies;
if (origDep != null) {
Set<String> newDep = new HashSet<String>(origDep.size());
for (String depName : origDep) {
ScriptCacheEntry dep = scriptCache.get(depName);
try {
if (origEntry == dep || GroovyScriptEngine.this.isSourceNewer(dep)) {
newDep.add(depName);
}
} catch (ResourceException re) {
}
}
StringSetMap cache = localData.dependencyCache;
cache.put(".", newDep);
}
}
示例8: assertExecute
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
protected void assertExecute(final String scriptStr, String codeBase, final Permission missingPermission) {
if (!isSecurityAvailable()) {
return;
}
final String effectiveCodeBase = (codeBase != null) ? codeBase : "/groovy/security/test";
// Use our privileged access in order to prevent checks lower in the call stack. Otherwise we would have
// to grant access to IDE unit test runners and unit test libs. We only care about testing the call stack
// higher upstream from this point of execution.
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
parseAndExecute(new GroovyCodeSource(scriptStr, generateClassName(), effectiveCodeBase), missingPermission);
return null;
}
});
}
示例9: parseScript
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
/**
* Parses a script
* @param clazzName
* @param sourceCode
* @return
*/
public String parseScript(String clazzName, String sourceCode) {
String compilationError = null;
int lastIndexOf = clazzName.lastIndexOf(".");
String codeBase = clazzName;
if (lastIndexOf!=-1) {
codeBase = clazzName.substring(0, lastIndexOf);
}
GroovyCodeSource groovyCodeSource = new GroovyCodeSource(
sourceCode, clazzName, codeBase);
try {
Class<?> parsedClass = groovyClassLoader.parseClass(groovyCodeSource, false);
availableClasses.put(clazzName, parsedClass);
} catch (CompilationFailedException e) {
compilationError = "Compilation for " + clazzName + " failed with " + e.getMessage();
LOGGER.warn(compilationError);
} catch (Exception ex) {
compilationError = "Parsing class " + clazzName + " failed with " + ex.getMessage();
LOGGER.warn(compilationError);
}
return compilationError;
}
示例10: compileScript
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@SuppressWarnings("unchecked")
protected Class<Script> compileScript(final String scriptBaseClass, String scriptSource, final String scriptName) {
final String script = preProcessScript(scriptSource);
GroovyCodeSource codeSource = AccessController.doPrivileged((PrivilegedAction<GroovyCodeSource>)
() -> new GroovyCodeSource(script, scriptName, getScriptCodeBase()));
String currentScriptBaseClass = compilerConfiguration.getScriptBaseClass();
try {
if (scriptBaseClass != null) {
compilerConfiguration.setScriptBaseClass(scriptBaseClass);
}
return groovyClassLoader.parseClass(codeSource, false);
}
finally {
compilerConfiguration.setScriptBaseClass(currentScriptBaseClass);
}
}
示例11: start
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Override
public void start() {
try {
String t_pathname = ContextUtils.getGroovyClasspath().getPath() + "/DailyWeekJob.groovy";
File file = new File(t_pathname);
Class groovyClass = ContextUtils.getClassLoader().loadClass("DailyWeekJob", true, false, true);
if (null == groovyClass) {
groovyClass = ContextUtils.getGroovyScriptEngine().loadScriptByName("DailyWeekJob.groovy");
}
if (null == groovyClass) {
groovyClass = ContextUtils.getClassLoader().parseClass(new GroovyCodeSource(file));
}
//
GroovyObject object = (GroovyObject) groovyClass.newInstance();
object.invokeMethod("schedule", "daily.mail");
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
status = ModuleStatus.STARTED;
}
示例12: getContingencies
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Override
public List<Contingency> getContingencies(Network network) {
try (Reader reader = Files.newBufferedReader(dslFile, StandardCharsets.UTF_8)) {
ActionDb actionDb = new ActionDslLoader(new GroovyCodeSource(reader, "script", GroovyShell.DEFAULT_CODE_BASE))
.load(network);
return ImmutableList.copyOf(actionDb.getContingencies());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
示例13: testGeneratorScalableStack
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Test
public void testGeneratorScalableStack() {
ActionDb actionDb = new ActionDslLoader(new GroovyCodeSource(getClass().getResource("/scalable.groovy"))).load(network);
Action action = actionDb.getAction("actionScale"); // scale to 15000
assertEquals(607.0f, g1.getTargetP(), 0.0f);
assertEquals(9999.99f, g1.getMaxP(), 0.0f);
action.run(network, null);
assertEquals(9999.99f, g1.getTargetP(), 0.0f);
}
示例14: testCompatible
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Test
public void testCompatible() {
ActionDb actionDb = new ActionDslLoader(new GroovyCodeSource(getClass().getResource("/scalable.groovy"))).load(network);
Action action = actionDb.getAction("testCompatible"); // scale to 15000
assertEquals(607.0f, g1.getTargetP(), 0.0f);
assertEquals(9999.99f, g1.getMaxP(), 0.0f);
action.run(network, null);
assertEquals(9999.99f, g1.getTargetP(), 0.0f);
}
示例15: testGeneratorScalableProportional
import groovy.lang.GroovyCodeSource; //导入依赖的package包/类
@Test
public void testGeneratorScalableProportional() {
ActionDb actionDb = new ActionDslLoader(new GroovyCodeSource(getClass().getResource("/scalable.groovy"))).load(network);
Action action = actionDb.getAction("testProportional"); // scale to 15000
assertEquals(607.0f, g1.getTargetP(), 0.0f);
assertEquals(9999.99f, g1.getMaxP(), 0.0f);
action.run(network, null);
assertEquals(7500.0f, g1.getTargetP(), 0.0f);
assertEquals(3000.0f, g2.getTargetP(), 0.0f);
assertEquals(4500.0f, g3.getTargetP(), 0.0f);
}