This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
DroolsAgentFactory.java
181 lines (150 loc) · 7.88 KB
/
DroolsAgentFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright 2011 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.mas.core;
import org.drools.runtime.StatefulKnowledgeSession;
import java.util.HashMap;
import javax.persistence.Persistence;
import org.drools.SystemEventListenerFactory;
import org.drools.grid.Grid;
import org.drools.grid.GridServiceDescription;
import org.drools.grid.conf.GridPeerServiceConfiguration;
import org.drools.grid.conf.impl.GridPeerConfiguration;
import org.drools.grid.impl.GridImpl;
import org.drools.grid.impl.MultiplexSocketServerImpl;
import org.drools.grid.io.impl.MultiplexSocketServiceConfiguration;
import org.drools.grid.remote.mina.MinaAcceptorFactoryService;
import org.drools.grid.service.directory.WhitePages;
import org.drools.grid.service.directory.impl.CoreServicesLookupConfiguration;
import org.drools.grid.service.directory.impl.JpaWhitePages;
import org.drools.grid.service.directory.impl.WhitePagesLocalConfiguration;
import org.drools.impl.StatefulKnowledgeSessionImpl;
import org.drools.management.DroolsManagementAgent;
import org.drools.mas.AgentID;
import org.drools.mas.util.helper.SessionLocator;
import org.drools.runtime.Globals;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Factory class. Creates Drools Agents out of a configuration object defining
* how many Knowledge Sessions, deployed on grid, will be available to a new
* agent itself. The sessions are built using the resources specified in the
* configuration file
*
*
* TODO at the moment, a single default grid node is created locally. TODO the
* topology is static, i.e. one master session with unidrectional links to
* "slave" sessions --> full GRID capabilities are being used to manage
* sessions dynamically
*/
public class DroolsAgentFactory {
private static Logger logger = LoggerFactory.getLogger(DroolsAgentFactory.class);
private static DroolsAgentFactory singleton;
public static DroolsAgentFactory getInstance() {
if (singleton == null) {
singleton = new DroolsAgentFactory();
}
return singleton;
}
private DroolsAgentFactory() {
}
public DroolsAgent spawn( DroolsAgentConfiguration config ) {
Grid grid = new GridImpl( "peer-" + config.getAgentId(), new HashMap<String, Object>() );
configureGrid( grid, config.getPort() );
AgentID aid = new AgentID();
aid.setName( config.getAgentId() );
aid.setLocalName( config.getAgentId() );
if ( logger.isInfoEnabled() ) {
logger.info( " >>> Spawning Agent => Name: " + aid.getName() );
}
try {
if ( logger.isDebugEnabled() ) {
logger.debug(" ### Creating Agent Mind: " + config.getAgentId() + "- CS: " + config.getChangeset() +" - mind location: " +config.getMindNodeLocation() );
}
SessionManager manager = SessionManager.create( config, null, grid, false );
if ( manager == null ) {
logger.error( "SOMETHING BAD HAPPENED WHILE TRYING TO CREATE AN AGENT, could not create sessionManager" );
grid.dispose();
return null;
}
StatefulKnowledgeSession mind = manager.getStatefulKnowledgeSession();
DroolsManagementAgent kmanagement = DroolsManagementAgent.getInstance();
//kmanagement.registerKnowledgeBase((ReteooRuleBase) ((KnowledgeBaseImpl) mind.get).getRuleBase());
kmanagement.registerKnowledgeSession( ( (StatefulKnowledgeSessionImpl) mind ).getInternalWorkingMemory() );
if ( logger.isDebugEnabled() ) {
logger.debug( " ### Mind Created: " + config.getAgentId() + "- CS: " + config.getChangeset() + " in "+ config.getMindNodeLocation() );
logger.debug( " ### Creating Agent Sub-Sessions " );
}
mind.setGlobal( "grid", grid );
for ( DroolsAgentConfiguration.SubSessionDescriptor descr : config.getSubSessions() ) {
if ( logger.isDebugEnabled() ) {
logger.debug( " ### Creating Agent Sub-Session: " + descr.getSessionId() + "- CS: " + descr.getChangeset() + " - on node: " + descr.getNodeId() );
}
SessionManager sm = SessionManager.create( config, descr, grid, true );
StatefulKnowledgeSession mindSet = sm.getStatefulKnowledgeSession();
// try{
// mindSet.setGlobal( "grid", grid );
// } catch (Exception e){
// //maybe 'grid' is not even defined in subsession
// logger.debug("Global 'grid' not set on session '"+descr.getSessionId()+"' due to "+e.getMessage());
// }
mindSet.fireAllRules();
mindSet.insert( new SessionLocator( config.getMindNodeLocation(), config.getAgentId(), true, false ) );
mindSet.insert( new SessionLocator( descr.getNodeId(), descr.getSessionId(), false, true ) );
mind.insert( new SessionLocator( descr.getNodeId(), descr.getSessionId(), false, true ) );
}
mind.insert( aid );
//Insert configuration as a fact inside the mind session
mind.insert( config );
mind.insert( new SessionLocator( config.getMindNodeLocation(), config.getAgentId(), true, false ) );
mind.fireAllRules();
return new DroolsAgent( grid, aid, mind );
} catch ( Throwable t ) {
if (t.getCause() != null){
logger.error( "SOMETHING BAD HAPPENED WHILE TRYING TO CREATE AN AGENT " + t.getMessage() + ", due to " + t.getCause().getMessage() );
}else{
logger.error( "SOMETHING BAD HAPPENED WHILE TRYING TO CREATE AN AGENT " + t.getMessage());
}
if ( logger.isDebugEnabled() ) {
t.printStackTrace();
}
grid.dispose();
}
return null;
}
private void configureGrid( Grid grid, int port ) {
//Local Grid Configuration, for our client
GridPeerConfiguration conf = new GridPeerConfiguration();
//Configuring the Core Services White Pages
GridPeerServiceConfiguration coreSeviceWPConf = new CoreServicesLookupConfiguration( new HashMap<String, GridServiceDescription>() );
conf.addConfiguration( coreSeviceWPConf );
//Configuring the a local WhitePages service that is being shared with all the grid peers
WhitePagesLocalConfiguration wplConf = new WhitePagesLocalConfiguration();
//wplConf.setWhitePages(new WhitePagesImpl());
wplConf.setWhitePages( new JpaWhitePages( Persistence.createEntityManagerFactory( "org.drools.grid" ) ) );
conf.addConfiguration( wplConf );
if ( port >= 0 ) {
//Configuring the SocketService
MultiplexSocketServiceConfiguration socketConf = new MultiplexSocketServiceConfiguration( new MultiplexSocketServerImpl( "127.0.0.1",
new MinaAcceptorFactoryService(),
SystemEventListenerFactory.getSystemEventListener(),
grid ) );
socketConf.addService( WhitePages.class.getName(), wplConf.getWhitePages(), port );
// socketConf.addService( SchedulerService.class.getName(), schlConf.getSchedulerService(), port );
conf.addConfiguration( socketConf );
}
conf.configure( grid );
}
}