Permalink
Browse files

PMML : Support incremental addition of models to a KB

  • Loading branch information...
1 parent 30ee6c9 commit e43c02a632924c9c9a3674e260f685b280002817 @sotty sotty committed Aug 6, 2012
Showing with 86 additions and 33 deletions.
  1. +20 −0 drools-pmml/src/main/resources/org/drools/pmml_4_0/pmml_compiler.drl
  2. +6 −0 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/global/modelMark.drlt
  3. +3 −1 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/informer/modelMultiQuestion.drlt
  4. +3 −1 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/informer/modelQuestion.drlt
  5. +3 −1 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/informer/modelQuestionnaire.drlt
  6. +2 −1 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/clustering/clusteringInit.drlt
  7. +3 −3 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/neural/neuralBeans.drlt
  8. +3 −1 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/neural/neuralBuildSynapses.drlt
  9. +1 −0 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/regression/regParams.drlt
  10. +2 −1 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/svm/svmBuild.drlt
  11. +7 −7 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/svm/svmDeclare.drlt
  12. +6 −5 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/svm/svmParams.drlt
  13. +2 −2 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/tree/treeDeclare.drlt
  14. +2 −1 drools-pmml/src/main/resources/org/drools/pmml_4_0/templates/models/tree/treeInit.drlt
  15. +2 −0 drools-pmml/src/test/java/org/drools/pmml_4_0/predictive/models/ClusteringTest.java
  16. +21 −9 drools-pmml/src/test/java/org/drools/pmml_4_0/predictive/models/MultipleModelTest.java
@@ -2066,6 +2066,11 @@ when
$tree : TreeModel( $name : modelName )
then
utils.context = utils.compactUpperCase( $name );
+
+ HashMap map = new HashMap( 3 );
+ map.put( "name", utils.context );
+ map.put( "type","DecisionTree" );
+ applyTemplate( "modelMark.drlt", null, registry, map, theory );
end
query treeParent( Node $n, Node $par )
@@ -2439,6 +2444,11 @@ when
$svm : SupportVectorMachineModel( $name : modelName )
then
utils.context = utils.compactUpperCase( $name );
+
+ HashMap map = new HashMap( 3 );
+ map.put( "name", utils.context );
+ map.put( "type","SupportVectorMachine" );
+ applyTemplate( "modelMark.drlt", null, registry, map, theory );
end
@@ -2707,6 +2717,11 @@ when
$reg : RegressionModel( $name : modelName )
then
utils.context = utils.compactUpperCase( $name );
+
+ HashMap map = new HashMap( 3 );
+ map.put( "name", utils.context );
+ map.put( "type","SimpleRegression" );
+ applyTemplate( "modelMark.drlt", null, registry, map, theory );
end
@@ -2878,6 +2893,11 @@ when
$klu : ClusteringModel( $name : modelName )
then
utils.context = utils.compactUpperCase( $name );
+
+ HashMap map = new HashMap( 3 );
+ map.put( "name", utils.context );
+ map.put( "type","Clustering" );
+ applyTemplate( "modelMark.drlt", null, registry, map, theory );
end
@@ -18,12 +18,18 @@
@declare{'modelMarkRule'}
rule "Mark_@{name}"
+salience -9999
when
+ not ModelMarker( "@{name}" ; )
then
ModelMarker mark = new ModelMarker( "@{name}", "@{type}" );
insert( mark );
end
+query modelMarker( String $model, ModelMarker $mm )
+ $mm := ModelMarker( $model ; )
+end
+
@end{}
@@ -30,6 +30,8 @@ dialect "mvel"
no-loop
salience 99
when
+ ModelMarker( "@{model}" ; )
+ not Question( type == @{ format( "string", model + "_" + name ) } )
then
MultipleChoiceQuestion quest = new MultipleChoiceQuestion(@{format("string",model)}+"_"+@{format("string",name)});
quest.setContext(@{format("string",model)});
@@ -53,7 +55,7 @@ then
@end{","}
});
- insertLogical(quest);
+ insert(quest);
end
@end{}
@@ -29,12 +29,14 @@ rule "Model_Question_@{model}_@{name}_@{context}"
no-loop
salience 99
when
+ ModelMarker( "@{model}" ; )
+ not Question( type == @{ format( "string", model + "_" + name ) } )
then
Question quest = new Question(@{format("string",model)}+"_"+@{format("string",name)});
quest.setContext(@{format("string",model)});
quest.setAnswerType(@{mapDatatypeToQuestion(dataType)});
quest.setPreLabel("Please insert " + @{format("string",displayName)});
- insertLogical(quest);
+ insert(quest);
end
@end{}
@@ -30,14 +30,16 @@ dialect "mvel"
no-loop
salience 1000
when
+ ModelMarker( "@{model}" ; )
+ not Questionnaire( type == @{ format( "string", model + "_Questionnaire" ) } )
then
Questionnaire questionnaire = new Questionnaire(@{format("string",model)}+"_Questionnaire");
questionnaire.setContext(@{format("string",model)});
questionnaire.setLabel(@{format("string",model)} + " form");
questionnaire.setItems({ @foreach{fld : fields} @{format("string",model)}+"_"+@{format("string",compactUpperCase(fld.name))} @end{","} });
- insertLogical(questionnaire);
+ insert(questionnaire);
end
@end{}
@@ -26,6 +26,7 @@
rule "Init Clusters @{context}"
when
+ ModelMarker( "@{context}" ; )
then
java.util.List clusters = new java.util.ArrayList();
ClusterSet model = new ClusterSet(
@@ -53,7 +54,7 @@ then
} @end{}
);
clusters.add( cluster@{j} );
- insert( cluster@{j} );
+ insertLogical( cluster@{j} );
@end{}
insert( model );
@@ -34,9 +34,9 @@ declare Stym
end
declare Synapse
- context : String
- source : String = "-1"
- target : String = "-1"
+ context : String @key
+ source : String = "-1" @key
+ target : String = "-1" @key
weight : double
end
@@ -23,7 +23,9 @@
@declare{'neuralBuildSynapsesRule'}
rule "BuildSynapses_@{model}_Neuron@{index}"
+no-loop
when
+ ModelMarker( "@{model}" ; )
then
Synapse syn;
@foreach{ con : links }
@@ -32,7 +34,7 @@ then
syn.setTarget(@{format("string",index)});
syn.setContext(@{format("string",model)});
syn.setWeight(@{con.weight});
- insert(syn);
+ insertLogical(syn);
@end{}
end
@@ -26,6 +26,7 @@
rule "Init Regression Model @{context} : @{target} - @{category}"
when
+ ModelMarker( "@{context}" ; )
then
insert( new RegTable( "@{context}",
@if{ target != null } "@{target}" @else{} null @end{},
@@ -26,8 +26,9 @@
rule "Init Svm @{svmId}"
when
+ ModelMarker( "@{context}" ; )
then
- insert( new SVM ( "@{svmId}",
+ insertLogical( new SVM ( "@{svmId}",
"@{modelName}",
"@{target}",
"@{alt}",
@@ -31,8 +31,8 @@ declare InputVector
end
declare SupportVector
- context : String
- id : String
+ context : String @key
+ id : String @key
index : int
value : double[]
end
@@ -45,16 +45,16 @@ declare KernelEval
end
declare SvmParams
- context : String
+ context : String @key
kt : String
gamma : double
coef0 : double
degree : double
end
declare SVM
- svmId : String
- context : String
+ svmId : String @key
+ context : String @key
target : String
alt : String
numSvs : int
@@ -64,8 +64,8 @@ declare SVM
end
declare SVMVote
- svmId : String
- context : String
+ svmId : String @key
+ context : String @key
target : String
end
@@ -20,12 +20,13 @@
rule "Init SvmParams @{modelName}"
when
+ ModelMarker( "@{context}" ; )
then
- insert( new SvmParams ( "@{modelName}",
- "@{kernelType}",
- @{gamma},
- @{coef0},
- @{degree}));
+ insertLogical( new SvmParams ( "@{modelName}",
+ "@{kernelType}",
+ @{gamma},
+ @{coef0},
+ @{degree} ) );
end
@end{}
@@ -37,8 +37,8 @@ declare TreeToken
end
declare TreeNode
- context : String
- id : String
+ context : String @key
+ id : String @key
parent : String
children : java.util.List
outcome : String
@@ -26,6 +26,7 @@
rule " Create tree @{context}"
when
+ ModelMarker( "@{context}" ; )
then
TreeNode node;
java.util.Map count;
@@ -47,7 +48,7 @@ then
confs,
"@{node.defaultChild}"
);
- insert( node );
+ insertLogical( node );
@end{}
end
@@ -27,6 +27,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
public class ClusteringTest extends DroolsAbstractPMMLTest {
@@ -55,6 +56,7 @@ public void testCenterBasedClustering() throws Exception {
FactType mu = kSession.getKnowledgeBase().getFactType( packageName, "DistanceMembership" );
Collection mus = kSession.getObjects( new ClassObjectFilter( mu.getFactClass()) );
+ assertTrue( mus.size() > 0 );
for ( Object x : mus ) {
Integer ix = (Integer) mu.get( x, "index" );
String lab = (String) mu.get( x, "label" );
@@ -31,7 +31,10 @@
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.type.FactType;
+import org.drools.event.rule.DebugAgendaEventListener;
+import org.drools.event.rule.DebugWorkingMemoryEventListener;
import org.drools.informer.Answer;
+import org.drools.informer.Questionnaire;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;
import org.drools.io.impl.ChangeSetImpl;
@@ -63,48 +66,57 @@
@Test
- public void testKnowledgeAgentLoading() throws Exception {
+ public void testKnowledgeAgentLoadingMultipleANN() throws Exception {
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KnowledgeAgentConfiguration kaConfig = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
kaConfig.setProperty( NewInstanceOption.PROPERTY_NAME, "false" );
kaConfig.setProperty( UseKnowledgeBaseClassloaderOption.PROPERTY_NAME, "true" );
KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "testPmml", kbase, kaConfig );
+ StatefulKnowledgeSession kSession = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
+ assertNotNull(kSession);
+
ChangeSetHelperImpl cs;
ClassPathResource res;
cs = new ChangeSetHelperImpl();
- res = (ClassPathResource) ResourceFactory.newClassPathResource(source1);
+ res = (ClassPathResource) ResourceFactory.newClassPathResource( source1 );
res.setResourceType( ResourceType.PMML );
cs.addNewResource( res );
kagent.applyChangeSet( cs.getChangeSet() );
+ kSession.fireAllRules();
+
System.out.println( " \n\n\n DONE LOADING " + source1 + " \n\n\n " );
cs = new ChangeSetHelperImpl();
- res = (ClassPathResource) ResourceFactory.newClassPathResource(source2);
+ res = (ClassPathResource) ResourceFactory.newClassPathResource( source2 );
res.setResourceType( ResourceType.PMML );
cs.addNewResource( res );
kagent.applyChangeSet( cs.getChangeSet() );
+ kSession.fireAllRules();
System.out.println( " \n\n\n DONE LOADING " + source2 + " \n\n\n " );
cs = new ChangeSetHelperImpl();
- res = (ClassPathResource) ResourceFactory.newClassPathResource(source3);
+ res = (ClassPathResource) ResourceFactory.newClassPathResource( source3 );
res.setResourceType( ResourceType.PMML );
cs.addNewResource( res );
kagent.applyChangeSet( cs.getChangeSet() );
+ kSession.fireAllRules();
System.out.println( " \n\n\n DONE LOADING " + source3 + " \n\n\n " );
- StatefulKnowledgeSession kSession = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
-
kSession.fireAllRules();
- assertNotNull( kSession );
- Collection markers = kSession.getObjects(new ClassObjectFilter(ModelMarker.class));
- assertEquals( 3, markers.size() );
+ System.err.println(reportWMObjects(kSession));
+
+ assertEquals( 3, kSession.getObjects( new ClassObjectFilter( ModelMarker.class ) ).size() );
+ assertEquals( 3, kSession.getObjects( new ClassObjectFilter( Questionnaire.class ) ).size() );
+ assertEquals( 23, kSession.getObjects( new ClassObjectFilter( kSession.getKnowledgeBase().getFactType( packageName, "Synapse" ).getFactClass() ) ).size() );
+
+
}

0 comments on commit e43c02a

Please sign in to comment.