使用Groovy脚本语言

时间:2015-07-07 11:35:40

标签: java groovy

我定义了自己的语法。我正在尝试使用Groovy脚本语言来评估派生。 这是我的班级:

package org.moeaframework.examples.ga.EvaluationRule;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Random;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import org.moeaframework.core.Problem;
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.Grammar;
import org.moeaframework.util.grammar.ContextFreeGrammar;
import org.moeaframework.util.grammar.Parser;
//import org.moeaframework.util.Vector;
public  class Myprob2 implements Problem   {
    static Random number_generator = new Random();
    private ContextFreeGrammar grammar;
    int source_index0 =  number_generator.nextInt(Input.Context().length);
    //String Cont = null;
    int source_index1 =  number_generator.nextInt(Input.Context().length);
    //String metr = null;
    int source_index2 =  number_generator.nextInt(Input.Context().length);
    //String prob = null;
    //double source_index = number_generator.nextDouble();
    static final String GRAMMAR = 
            "<rules> ::= 'IF' ' ' <context> ' ' <op>  ' ' <val> ' and ' <metrics>' ' <op> ' '<val1> ' Then ' <problem> \n" + 
            "<context> ::= Cont \n" + 
            "<op> ::= '>' | '<' \n" + 
            "<val> ::= 'H' | 'M' | 'L' \n" +  
            "<metrics> ::= metr \n" + 
            "<val1> ::= source_index \n" +  
            "<problem> ::= prob ";
    public Myprob2() {
             super();
                try {
                    grammar = Parser.load( new StringReader(GRAMMAR));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }
    ArrayList<String> strs =new ArrayList<String>();
    public void evaluate(Solution solution)  {
         int nbr_prob =0;
         String result = null;
         int[] codon = ((Grammar)solution.getVariable(0)).toArray();
         // derive the program using the codon
         String program = grammar.build(codon);
         //System.out.println("$$$$$$$$$$"+program);
        if (program == null) {
          // if null, the codon did not produce a valid grammar
         // TODO: penalize the objective value
    } else {
        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine engine = sem.getEngineByName("groovy");
         // initialize the variables used by the program
         Bindings b = new SimpleBindings();
         b.put("Cont", Input.Context()[source_index0]);
         b.put("metr", Input.Metrics()[source_index1]);
         b.put("source_index", number_generator.nextDouble());
         b.put("prob", Input.Problem()[source_index2]);

         try {
             result = ((String)engine.eval(program, b)).toString();
        } catch (ScriptException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         //System.out.println("tessttt"+b);
         // TODO: use the result to set the objective value
         strs.add(result);
     //System.out.println(str);
     for(int i=0; i<strs.size();i++)
     {  String str2 = strs.get(i);
        for(int j=1; j<strs.size()-1;j++)
        { if(strs.get(j).compareTo(str2)!=0)
        {  nbr_prob++; }
            }
     }
     solution.setObjective(0, -nbr_prob);
     //solution.setObjective(0,rules.size());

     }
    }

    @Override
    public String getName() {
        return "Myprob";
    }


    public Solution newSolution() {
        Solution solution = new Solution(1, 1);
         solution.setVariable(0, new Grammar(10));      
        return solution;
             }


    @Override
    public void close() {
        //do nothing
    }
    }
}

我收到以下错误:

        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:151)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at org.moeaframework.examples.ga.EvaluationRule.Myprob2.evaluate(Myprob2.java:97)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:99)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:1)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    Script59.groovy: 1: unexpected token: Cont @ line 1, column 4.
       IF Cont > M and metr > source_index Then prob
          ^

    1 error

        at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
        at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
        at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:142)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108)
        at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
        at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:164)
        at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:928)
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:590)
        at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:566)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:543)
        at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:297)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:253)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:211)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:366)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:143)
        ... 8 more
    javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: H for class: Script56
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:151)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at org.moeaframework.examples.ga.EvaluationRule.Myprob2.evaluate(Myprob2.java:97)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:99)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:1)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: H for class: Script56
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:347)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:145)
        ... 8 more
    Caused by: groovy.lang.MissingPropertyException: No such property: H for class: Script56
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:51)
        at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:304)
        at Script56.run(Script56.groovy:1)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:344)
        ... 9 more
    javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: L for class: Script58
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:151)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at org.moeaframework.examples.ga.EvaluationRule.Myprob2.evaluate(Myprob2.java:97)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:99)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:1)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: L for class: Script58
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:347)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:145)
        ... 8 more
    Caused by: groovy.lang.MissingPropertyException: No such property: L for class: Script58
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:51)
        at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:304)
        at Script58.run(Script58.groovy:1)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:344)
        ... 9 more
    javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: L for class: Script54
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:151)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at org.moeaframework.examples.ga.EvaluationRule.Myprob2.evaluate(Myprob2.java:97)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:99)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:1)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: L for class: Script54
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:347)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:145)
        ... 8 more
    Caused by: groovy.lang.MissingPropertyException: No such property: L for class: Script54
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:51)
        at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:304)
        at Script54.run(Script54.groovy:1)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:344)
        ... 9 more
    javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    Script62.groovy: 1: unexpected token: Cont @ line 1, column 4.
       IF Cont > L and metr < source_index Then prob
          ^

    1 error

        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:151)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at org.moeaframework.examples.ga.EvaluationRule.Myprob2.evaluate(Myprob2.java:97)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:99)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:1)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    Script62.groovy: 1: unexpected token: Cont @ line 1, column 4.
       IF Cont > L and metr < source_index Then prob
          ^

    1 error

        at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
        at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
        at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:142)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108)
        at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
        at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:164)
        at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:928)
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:590)
        at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:566)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:543)
        at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:297)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:253)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:211)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:366)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:143)
        ... 8 more
    javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    Script63.groovy: 1: unexpected token: Cont @ line 1, column 4.
       IF Cont > M and metr < source_index Then prob
          ^

    1 error

        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:151)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at org.moeaframework.examples.ga.EvaluationRule.Myprob2.evaluate(Myprob2.java:97)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:99)
        at org.moeaframework.util.distributed.DistributedProblem$ProblemEvaluator.call(DistributedProblem.java:1)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    Script63.groovy: 1: unexpected token: Cont @ line 1, column 4.
       IF Cont > M and metr < source_index Then prob
          ^

    1 error

        at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
        at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
        at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:142)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108)
        at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
        at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:164)
        at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:928)
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:590)
        at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:566)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:543)
        at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:297)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:253)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:211)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:366)
        at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:143)
        ... 8 more
    javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    Script64.groovy: 1: unexpected token: Cont @ line 1, column 4.
       IF Cont > L and metr > source_index Then prob
          ^
  ....................

请给我解决这个问题的想法。谢谢!!

0 个答案:

没有答案