[rules-users] KnowledgeAgent to update knowledge base built with Guvnor pkg

riri irina.adam at gmail.com
Thu May 2 04:52:07 EDT 2013


Here is the code I am using to create the knowledge base, knowledge agent and
to retrieve the resources:

public static KnowledgeBase createKnowledgeBase(List<DroolsResource>
resources, 
			 EventProcessingOption eventProcessingOption)
	{
		KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		
		for (DroolsResource resource : resources) 
		{
			logger.info("Resource: " + resource.getType() + ", path type="
	                + resource.getPathType() + ", path=" + resource.getPath());
	        switch (resource.getPathType()) 
	        {
		        case CLASSPATH:
		           
builder.add(ResourceFactory.newClassPathResource(resource.getPath()),
		            		resource.getType());
		            break;
		        case FILE:
		           
builder.add(ResourceFactory.newFileResource(resource.getPath()),
		            		resource.getType());
		            break;
		        case URL:
		            UrlResource urlResource = (UrlResource) ResourceFactory
		                    .newUrlResource(resource.getPath());
		            
		            if (resource.getUsername() != null) 
		            {
		                logger.info("Setting authentication for: " +
resource.getUsername());
		                urlResource.setBasicAuthentication("enabled");
		                urlResource.setUsername(resource.getUsername());
		                urlResource.setPassword(resource.getPassword());
		            }
		            
		            builder.add(urlResource, resource.getType());
		            break;
		        default:
		        	throw new IllegalArgumentException(
	                    "Unable to build this resource path type.");
	        }
        }
		
		if (builder.hasErrors()) {
            throw new RuntimeException(builder.getErrors().toString());
        }

        KnowledgeBaseConfiguration conf =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
       // setting the STREAM option
        conf.setOption(eventProcessingOption);
        // modify the default identity compare mode to equality
        conf.setOption(AssertBehaviorOption.EQUALITY);
        
        KnowledgeBase knowledgeBase =
KnowledgeBaseFactory.newKnowledgeBase(conf);
        knowledgeBase.addKnowledgePackages(builder.getKnowledgePackages());

        return knowledgeBase;
		
	}

    public static KnowledgeAgent createKnowledgeAgent(String
knowledgeAgentName, KnowledgeBase knowledgeBase)
	{
		KnowledgeAgentConfiguration aconf =
KnowledgeAgentFactory.newKnowledgeAgentConfiguration();

		aconf.setProperty("drools.agent.newInstance", "false"); 
		KnowledgeAgent kagent =
KnowledgeAgentFactory.newKnowledgeAgent(knowledgeAgentName, knowledgeBase,
aconf);
		
		ResourceFactory.getResourceChangeScannerService().start();
		ResourceFactory.getResourceChangeNotifierService().start();

		return kagent;
	}

    public DroolsResource getGuvnorResource(User user)
	{
		String packageName = user.getUsername();
		String guvnorBinaryResourceUrl =
"http://127.0.0.1:8080/guvnor/rest/packages/" + packageName + "/binary";
		return new DroolsResource(guvnorBinaryResourceUrl, ResourcePathType.URL,
ResourceType.PKG, credentials.getUserPrincipal().getName(),
credentials.getPassword());	
	}

Where the DroolsResource is a custom class to hold information about a
resource:

public class DroolsResource
{

	private String path;
	private ResourcePathType pathType;
	private ResourceType type;
	private String username = null;
	private String password = null;

     //getters, setters and constructors
}

To initialize the environement I just call the method to create the
knowledge base once I have the DroolsResource and then the method to create
the knowledge agent. Afterwards I create a statefull knowledge session using
the getKnowledgeBase() of the agent. Would the problem be the Guvnor url
that I am using? I used the rest version since it sais that it builds the
package automatically. Or the fact that the url is not in a change-set.xml
file? 

When I run the session the first time (via the fireUntilHalt() in a separate
thread), rules get executed correctly and I call the halt() method to stop
it. If I then go and change a rule in the package and restart the session
then no changes are taken into account. Is the way I am executing the
sessions a problem? I do not want to recreate a new session every time since
I would have to reinsert all the facts, so I retrieve the session from the
knowledge base. I dispose of the session when the application is shut down.
I am new at this so I might be overlooking an important aspect.

Many thanks for all your help.




--
View this message in context: http://drools.46999.n3.nabble.com/Re-rules-users-KnowledgeAgent-to-update-knowledge-base-built-with-Guvnor-pkg-tp4023608p4023624.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list