Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-383

switch over String in function

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 6.3.0.Beta2
    • 6.0.0.Final
    • None
    • None

      Executive Summary: if I try to implement switch over String as per Java 1.7, inside of DRL function, compilation of DRL fails, but the error message would suggest that actually switch over String should work.

      Steps to reproduce
      Have a DRL file with function with String parameter and inside the function a switch over the parameter, for example

      acme.drl
      rule "Stupid rule over String"
      when
      	String()
      then
      	System.out.println("Hello world");
      end
      
      function void theTest(String input) {
      	switch(input) {
      		case "Hello World" : 
      			System.out.println("yep");
      			break;
      		default :
      			System.out.println("uh");
      			break;
      	}
      }
      

      Have simple code to compile DRL for default KieBase, for example snippet:

      App.java
      String input = "Hello World";
      // Java 1.7
      switch(input) {
      	case "Hello World" : 
      		System.out.println("yep");
      		break;
      	default :
      		System.out.println("uh");
      		break;
      }
      System.out.println( "Hello World!" );
      
      KieServices ks = KieServices.Factory.get();
      KieContainer kContainer = ks.getKieClasspathContainer();
      KieBase kieBase = kContainer.getKieBase();
      
      for ( KiePackage kp : kieBase.getKiePackages() ) {
      	for (Rule rule : kp.getRules()) {
      		System.out.println("kp" + kp + " rule " + rule.getName());
      	}
      }
      

      Console would log DRL compile fails with error:

      Exception in thread "main" java.lang.RuntimeException: Error while creating KieBase[Message [id=1, level=ERROR, path=acme.drl, line=-1, column=0
         text=Error importing : 'defaultpkg.TheTest.theTest'], Message [id=2, level=ERROR, path=acme.drl, line=8, column=0
         text=[ function theTesttheTest (line:8): Cannot switch on a value of type String. Only convertible int values, strings or enum constants are permitted
       ]], Message [id=3, level=ERROR, path=acme.drl, line=1, column=0
         text=Rule Compilation error The import defaultpkg.TheTest cannot be resolved]]
      

      Please notice the 'strings' bit in the error message : Cannot switch on a value of type String. Only convertible int values, strings or enum constants are permitted

      Therefore would suggest it would compile a switch over String, but for some reason it is failing to recognize that input parameter is indeed a String?

      Thank you
      Ciao
      MM

              psiroky_jira Petr Široký (Inactive)
              mmortari@redhat.com Matteo Mortari
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: