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 |
| package org.drools.io; |
41 |
| |
42 |
| import java.util.HashSet; |
43 |
| |
44 |
| import org.drools.rule.ApplicationData; |
45 |
| import org.drools.rule.Rule; |
46 |
| import org.drools.rule.RuleConstructionException; |
47 |
| import org.drools.rule.RuleSet; |
48 |
| import org.drools.smf.Configuration; |
49 |
| import org.drools.smf.FactoryException; |
50 |
| import org.drools.smf.RuleFactory; |
51 |
| import org.drools.smf.SemanticModule; |
52 |
| import org.drools.spi.Functions; |
53 |
| import org.drools.spi.ImportEntry; |
54 |
| import org.xml.sax.Attributes; |
55 |
| import org.xml.sax.SAXException; |
56 |
| import org.xml.sax.SAXParseException; |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| class RuleHandler extends BaseAbstractHandler |
65 |
| implements |
66 |
| Handler |
67 |
| { |
68 |
66
| RuleHandler(RuleSetReader ruleSetReader)
|
69 |
| { |
70 |
66
| this.ruleSetReader = ruleSetReader;
|
71 |
| |
72 |
66
| if ( (this.validParents == null) && (this.validPeers == null) )
|
73 |
| { |
74 |
66
| this.validParents = new HashSet( );
|
75 |
66
| this.validParents.add( RuleSet.class );
|
76 |
| |
77 |
66
| this.validPeers = new HashSet( );
|
78 |
66
| this.validPeers.add( null );
|
79 |
66
| this.validPeers.add( Rule.class );
|
80 |
66
| this.validPeers.add( ImportEntry.class );
|
81 |
66
| this.validPeers.add( ApplicationData.class );
|
82 |
66
| this.validPeers.add( Functions.class );
|
83 |
| |
84 |
66
| this.allowNesting = false;
|
85 |
| } |
86 |
| } |
87 |
| |
88 |
79
| public Object start(String uri,
|
89 |
| String localName, |
90 |
| Attributes attrs) throws SAXException |
91 |
| { |
92 |
| |
93 |
79
| SemanticModule module = this.ruleSetReader.lookupSemanticModule( uri,
|
94 |
| localName ); |
95 |
| |
96 |
79
| RuleFactory factory = module.getRuleFactory( localName );
|
97 |
| |
98 |
79
| this.ruleSetReader.startConfiguration( localName,
|
99 |
| attrs ); |
100 |
| |
101 |
79
| Configuration config = this.ruleSetReader.endConfiguration( );
|
102 |
| |
103 |
79
| Rule rule;
|
104 |
79
| try
|
105 |
| { |
106 |
79
| rule = factory.newRule( this.ruleSetReader.getRuleSet( ),
|
107 |
| this.ruleSetReader.getFactoryContext( ), |
108 |
| config ); |
109 |
| |
110 |
78
| startRule( rule,
|
111 |
| attrs ); |
112 |
| } |
113 |
| catch ( FactoryException e ) |
114 |
| { |
115 |
1
| throw new SAXParseException( "error constructing rule",
|
116 |
| this.ruleSetReader.getLocator( ), |
117 |
| e ); |
118 |
| } |
119 |
78
| return rule;
|
120 |
| } |
121 |
| |
122 |
78
| private void startRule(Rule rule,
|
123 |
| Attributes attrs) throws SAXException |
124 |
| { |
125 |
78
| String salienceStr = attrs.getValue( "salience" );
|
126 |
78
| String noLoopStr = attrs.getValue( "no-loop" );
|
127 |
78
| String ruleDesc = attrs.getValue( "description" );
|
128 |
| |
129 |
78
| if ( !(salienceStr == null || salienceStr.trim( ).equals( "" )) )
|
130 |
| { |
131 |
16
| try
|
132 |
| { |
133 |
16
| int salience = Integer.parseInt( salienceStr.trim( ) );
|
134 |
| |
135 |
16
| rule.setSalience( salience );
|
136 |
| } |
137 |
| catch ( NumberFormatException e ) |
138 |
| { |
139 |
0
| throw new SAXParseException( "invalid number value for 'salience' attribute: " + salienceStr.trim( ),
|
140 |
| this.ruleSetReader.getLocator( ) ); |
141 |
| } |
142 |
| } |
143 |
| |
144 |
78
| if ( !(noLoopStr == null || noLoopStr.trim( ).equals( "" )) )
|
145 |
| { |
146 |
0
| try
|
147 |
| { |
148 |
0
| boolean noLoop = new Boolean( noLoopStr.trim( ) ).booleanValue( );
|
149 |
0
| rule.setNoLoop( noLoop );
|
150 |
| } |
151 |
| catch ( NumberFormatException e ) |
152 |
| { |
153 |
0
| throw new SAXParseException( "invalid boolean value for 'no-loop' attribute: " + salienceStr.trim( ),
|
154 |
| this.ruleSetReader.getLocator( ) ); |
155 |
| } |
156 |
| } |
157 |
| |
158 |
78
| if ( !(ruleDesc == null || ruleDesc.trim( ).equals( "" )) )
|
159 |
| { |
160 |
15
| rule.setDocumentation( ruleDesc );
|
161 |
| } |
162 |
| |
163 |
78
| rule.setImporter( this.ruleSetReader.getRuleSet( ).getImporter( ) );
|
164 |
78
| rule.setApplicationData( this.ruleSetReader.getRuleSet( ).getApplicationData( ) );
|
165 |
| } |
166 |
| |
167 |
78
| public Object end(String uri,
|
168 |
| String localName) throws SAXException |
169 |
| { |
170 |
78
| try
|
171 |
| { |
172 |
78
| this.ruleSetReader.getRuleSet( ).addRule( (Rule) this.ruleSetReader.getParents( ).getLast( ) );
|
173 |
| } |
174 |
| catch ( RuleConstructionException e ) |
175 |
| { |
176 |
0
| throw new SAXParseException( "error adding rule",
|
177 |
| this.ruleSetReader.getLocator( ), |
178 |
| e ); |
179 |
| } |
180 |
| |
181 |
78
| return null;
|
182 |
| |
183 |
| } |
184 |
| |
185 |
157
| public Class generateNodeFor()
|
186 |
| { |
187 |
157
| return Rule.class;
|
188 |
| } |
189 |
| |
190 |
| } |