Source Code Cross Referenced for LogicFactorySupport.java in  » Rule-Engine » Mandarax » org » mandarax » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Rule Engine » Mandarax » org.mandarax.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003:         *
004:         * This library is free software; you can redistribute it and/or
005:         * modify it under the terms of the GNU Lesser General Public
006:         * License as published by the Free Software Foundation; either
007:         * version 2 of the License, or (at your option) any later version.
008:         *
009:         * This library is distributed in the hope that it will be useful,
010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:         * Lesser General Public License for more details.
013:         *
014:         * You should have received a copy of the GNU Lesser General Public
015:         * License along with this library; if not, write to the Free Software
016:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017:         */
018:        package org.mandarax.util;
019:
020:        import java.util.List;
021:        import java.util.Vector;
022:
023:        import org.mandarax.kernel.ComplexTerm;
024:        import org.mandarax.kernel.ConstantTerm;
025:        import org.mandarax.kernel.Fact;
026:        import org.mandarax.kernel.Function;
027:        import org.mandarax.kernel.LogicFactory;
028:        import org.mandarax.kernel.Predicate;
029:        import org.mandarax.kernel.Prerequisite;
030:        import org.mandarax.kernel.Query;
031:        import org.mandarax.kernel.Rule;
032:        import org.mandarax.kernel.Term;
033:
034:        /**
035:         * Utility class to facilitate creating rules, facts and terms.
036:         * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
037:         * @version 3.4 <7 March 05>
038:         * @since 1.2
039:         */
040:        public class LogicFactorySupport {
041:
042:            private LogicFactory lfactory = LogicFactory.getDefaultFactory();
043:
044:            /**
045:             * Constructor.
046:             */
047:            public LogicFactorySupport() {
048:                super ();
049:            }
050:
051:            /**
052:             * Constructor.
053:             * @param aFactory the logic factory to be used
054:             */
055:            public LogicFactorySupport(LogicFactory aFactory) {
056:                super ();
057:            }
058:
059:            /**
060:             * Create a constant term.
061:             * @return the constant term created
062:             * @param obj the object
063:             * @param type the type
064:             */
065:            public ConstantTerm cons(Object obj, Class type) {
066:                return lfactory.createConstantTerm(obj, type);
067:            }
068:
069:            /**
070:             * Create a complex term.
071:             * See <code>toTerm</code> how the object is converted to a term.
072:             * @return the complex term created
073:             * @param function the function
074:             * @param objs objects (will be interpreted as terms)
075:             */
076:            public ComplexTerm cplx(Function function, Object[] objs) {
077:                Class[] structure = function.getStructure();
078:
079:                if (structure.length != objs.length) {
080:                    throw new IllegalArgumentException(
081:                            "Not the right number of terms for this function, the number is "
082:                                    + objs.length + ", but must be "
083:                                    + structure.length);
084:                }
085:
086:                Term[] terms = new Term[structure.length];
087:
088:                for (int i = 0; i < structure.length; i++) {
089:                    terms[i] = toTerm(objs[i], structure[i]);
090:                }
091:
092:                return lfactory.createComplexTerm(function, terms);
093:            }
094:
095:            /**
096:             * Create a complex term with one (sub)term.
097:             * See <code>toTerm</code> how the object is converted to a term.
098:             * @return the complex term created
099:             * @param function the function
100:             * @param obj an object
101:             */
102:            public ComplexTerm cplx(Function function, Object obj1) {
103:                Class[] structure = function.getStructure();
104:
105:                if (structure.length != 1) {
106:                    throw new IllegalArgumentException(
107:                            "Not the right number of terms for this function, the number is 1, but must be "
108:                                    + structure.length);
109:                }
110:
111:                Term[] terms = { toTerm(obj1, structure[0]) };
112:
113:                return lfactory.createComplexTerm(function, terms);
114:            }
115:
116:            /**
117:             * Create a complex term with two (sub)terms.
118:             * See <code>toTerm</code> how the object is converted to a term.
119:             * @return the complex term created
120:             * @param function the function
121:             * @param obj1 an object
122:             * @param obj2 an object
123:             */
124:            public ComplexTerm cplx(Function function, Object obj1, Object obj2) {
125:                Class[] structure = function.getStructure();
126:
127:                if (structure.length != 2) {
128:                    throw new IllegalArgumentException(
129:                            "Not the right number of terms for this function, the number is 2, but must be "
130:                                    + structure.length);
131:                }
132:
133:                Term[] terms = { toTerm(obj1, structure[0]),
134:                        toTerm(obj2, structure[1]) };
135:
136:                return lfactory.createComplexTerm(function, terms);
137:            }
138:
139:            /**
140:             * Create a complex term with three (sub)terms.
141:             * See <code>toTerm</code> how the object is converted to a term.
142:             * @return the complex term created
143:             * @param function the function
144:             * @param obj1 an object
145:             * @param obj2 an object
146:             * @param obj3 an object
147:             */
148:            public ComplexTerm cplx(Function function, Object obj1,
149:                    Object obj2, Object obj3) {
150:                Class[] structure = function.getStructure();
151:
152:                if (structure.length != 3) {
153:                    throw new IllegalArgumentException(
154:                            "Not the right number of terms for this function, the number is 3, but must be "
155:                                    + structure.length);
156:                }
157:
158:                Term[] terms = { toTerm(obj1, structure[0]),
159:                        toTerm(obj2, structure[1]), toTerm(obj3, structure[2]) };
160:
161:                return lfactory.createComplexTerm(function, terms);
162:            }
163:
164:            /**
165:             * Create a complex term with four (sub)terms.
166:             * See <code>toTerm</code> how the object is converted to a term.
167:             * @return the complex term created
168:             * @param function the function
169:             * @param obj1 an object
170:             * @param obj2 an object
171:             * @param obj3 an object
172:             * @param obj4 an object
173:             */
174:            public ComplexTerm cplx(Function function, Object obj1,
175:                    Object obj2, Object obj3, Object obj4) {
176:                Class[] structure = function.getStructure();
177:
178:                if (structure.length != 4) {
179:                    throw new IllegalArgumentException(
180:                            "Not the right number of terms for this function, the number is 4, but must be "
181:                                    + structure.length);
182:                }
183:
184:                Term[] terms = { toTerm(obj1, structure[0]),
185:                        toTerm(obj2, structure[1]), toTerm(obj3, structure[2]),
186:                        toTerm(obj4, structure[3]) };
187:
188:                return lfactory.createComplexTerm(function, terms);
189:            }
190:
191:            /**
192:             * Create a fact.
193:             * See <code>toTerm</code> how the object is converted to a term.
194:             * @return the fact created
195:             * @param predicate the predicate
196:             * @param objs objects (will be interpreted as terms)
197:             */
198:            public Fact fact(Predicate predicate, Object[] objs) {
199:                Class[] structure = predicate.getStructure();
200:
201:                if (structure.length != objs.length) {
202:                    throw new IllegalArgumentException(
203:                            "Not the right number of terms for this predicate, the number is "
204:                                    + objs.length + ", but must be "
205:                                    + structure.length);
206:                }
207:
208:                Term[] terms = new Term[structure.length];
209:
210:                for (int i = 0; i < structure.length; i++) {
211:                    terms[i] = toTerm(objs[i], structure[i]);
212:                }
213:
214:                return lfactory.createFact(predicate, terms);
215:            }
216:
217:            /**
218:             * Create a fact with one term.
219:             * See <code>toTerm</code> how the object is converted to a term.
220:             * @return the fact created
221:             * @param predicate the predicate
222:             * @param obj an object
223:             */
224:            public Fact fact(Predicate predicate, Object obj) {
225:                Class[] structure = predicate.getStructure();
226:
227:                if (structure.length != 1) {
228:                    throw new IllegalArgumentException(
229:                            "Not the right number of terms for this predicate, the number is 1, but must be "
230:                                    + structure.length);
231:                }
232:
233:                Term[] terms = { toTerm(obj, structure[0]) };
234:
235:                return lfactory.createFact(predicate, terms);
236:            }
237:
238:            /**
239:             * Create a fact with two terms.
240:             * See <code>toTerm</code> how the object is converted to a term.
241:             * @return the fact created
242:             * @param predicate the predicate
243:             * @param obj1 an object
244:             * @param obj2 an object
245:             */
246:            public Fact fact(Predicate predicate, Object obj1, Object obj2) {
247:                Class[] structure = predicate.getStructure();
248:
249:                if (structure.length != 2) {
250:                    throw new IllegalArgumentException(
251:                            "Not the right number of terms for this predicate, the number is 2, but must be "
252:                                    + structure.length);
253:                }
254:
255:                Term[] terms = { toTerm(obj1, structure[0]),
256:                        toTerm(obj2, structure[1]) };
257:
258:                return lfactory.createFact(predicate, terms);
259:            }
260:
261:            /**
262:             * Create a fact with three terms.
263:             * See <code>toTerm</code> how the object is converted to a term.
264:             * @return the fact created
265:             * @param predicate the predicate
266:             * @param obj1 an object
267:             * @param obj2 an object
268:             * @param obj3 an object
269:             */
270:            public Fact fact(Predicate predicate, Object obj1, Object obj2,
271:                    Object obj3) {
272:                Class[] structure = predicate.getStructure();
273:
274:                if (structure.length != 3) {
275:                    throw new IllegalArgumentException(
276:                            "Not the right number of terms for this predicate, the number is 3, but must be "
277:                                    + structure.length);
278:                }
279:
280:                Term[] terms = { toTerm(obj1, structure[0]),
281:                        toTerm(obj2, structure[1]), toTerm(obj3, structure[2]) };
282:
283:                return lfactory.createFact(predicate, terms);
284:            }
285:
286:            /**
287:             * Create a fact with four terms.
288:             * See <code>toTerm</code> how the object is converted to a term.
289:             * @return the fact created
290:             * @param predicate the predicate
291:             * @param obj1 an object
292:             * @param obj2 an object
293:             * @param obj3 an object
294:             * @param obj4 an object
295:             */
296:            public Fact fact(Predicate predicate, Object obj1, Object obj2,
297:                    Object obj3, Object obj4) {
298:                Class[] structure = predicate.getStructure();
299:
300:                if (structure.length != 4) {
301:                    throw new IllegalArgumentException(
302:                            "Not the right number of terms for this predicate, the number is 4, but must be "
303:                                    + structure.length);
304:                }
305:
306:                Term[] terms = { toTerm(obj1, structure[0]),
307:                        toTerm(obj2, structure[1]), toTerm(obj3, structure[2]),
308:                        toTerm(obj4, structure[3]) };
309:
310:                return lfactory.createFact(predicate, terms);
311:            }
312:
313:            /**
314:             * Create a prerequisite.
315:             * See <code>toTerm</code> how the object is converted to a term.
316:             * @return the prerequisite created
317:             * @param predicate the predicate
318:             * @param objs objects (will be interpreted as terms)
319:             */
320:            public Prerequisite prereq(Predicate predicate, Object[] objs) {
321:
322:                return prereq(predicate, objs, false);
323:            }
324:
325:            /**
326:             * Create a prerequisite with one term.
327:             * See <code>toTerm</code> how the object is converted to a term.
328:             * @return the prerequisite created
329:             * @param predicate the predicate
330:             * @param obj an object
331:             */
332:            public Prerequisite prereq(Predicate predicate, Object obj) {
333:
334:                return prereq(predicate, obj, false);
335:            }
336:
337:            /**
338:             * Create a prerequisite with two terms.
339:             * See <code>toTerm</code> how the object is converted to a term.
340:             * @return the prerequisite created
341:             * @param predicate the predicate
342:             * @param obj1 an object
343:             * @param obj2 an object
344:             */
345:            public Prerequisite prereq(Predicate predicate, Object obj1,
346:                    Object obj2) {
347:
348:                return prereq(predicate, obj1, obj2, false);
349:            }
350:
351:            /**
352:             * Create a prerequisite with three terms.
353:             * See <code>toTerm</code> how the object is converted to a term.
354:             * @return the prerequisite created
355:             * @param predicate the predicate
356:             * @param obj1 an object
357:             * @param obj2 an object
358:             * @param obj3 an object
359:             */
360:            public Prerequisite prereq(Predicate predicate, Object obj1,
361:                    Object obj2, Object obj3) {
362:
363:                return prereq(predicate, obj1, obj2, obj3, false);
364:            }
365:
366:            /**
367:             * Create a prerequisite with four terms.
368:             * See <code>toTerm</code> how the object is converted to a term.
369:             * @return the prerequisite created
370:             * @param predicate the predicate
371:             * @param obj1 an object
372:             * @param obj2 an object
373:             * @param obj3 an object
374:             * @param obj4 an object
375:             */
376:            public Prerequisite prereq(Predicate predicate, Object obj1,
377:                    Object obj2, Object obj3, Object obj4) {
378:
379:                return prereq(predicate, obj1, obj2, obj3, obj4, false);
380:            }
381:
382:            ///
383:
384:            /**
385:             * Create a prerequisite.
386:             * See <code>toTerm</code> how the object is converted to a term.
387:             * @return the prerequisite created
388:             * @param predicate the predicate
389:             * @param objs objects (will be interpreted as terms)
390:             * @param negatedAF whether the prerequisite is negated (using negation as failure)
391:             */
392:            public Prerequisite prereq(Predicate predicate, Object[] objs,
393:                    boolean negatedAF) {
394:                Class[] structure = predicate.getStructure();
395:
396:                if (structure.length != objs.length) {
397:                    throw new IllegalArgumentException(
398:                            "Not the right number of terms for this predicate, the number is "
399:                                    + objs.length + ", but must be "
400:                                    + structure.length);
401:                }
402:
403:                Term[] terms = new Term[structure.length];
404:
405:                for (int i = 0; i < structure.length; i++) {
406:                    terms[i] = toTerm(objs[i], structure[i]);
407:                }
408:
409:                return lfactory.createPrerequisite(predicate, terms, negatedAF);
410:            }
411:
412:            /**
413:             * Create a prerequisite with one term.
414:             * See <code>toTerm</code> how the object is converted to a term.
415:             * @return the prerequisite created
416:             * @param predicate the predicate
417:             * @param obj an object
418:             * @param negatedAF whether the prerequisite is negated (using negation as failure)
419:             */
420:            public Prerequisite prereq(Predicate predicate, Object obj,
421:                    boolean negatedAF) {
422:                Class[] structure = predicate.getStructure();
423:
424:                if (structure.length != 1) {
425:                    throw new IllegalArgumentException(
426:                            "Not the right number of terms for this predicate, the number is 1, but must be "
427:                                    + structure.length);
428:                }
429:
430:                Term[] terms = { toTerm(obj, structure[0]) };
431:
432:                return lfactory.createPrerequisite(predicate, terms, negatedAF);
433:            }
434:
435:            /**
436:             * Create a prerequisite with two terms.
437:             * See <code>toTerm</code> how the object is converted to a term.
438:             * @return the prerequisite created
439:             * @param predicate the predicate
440:             * @param obj1 an object
441:             * @param obj2 an object
442:             * @param negatedAF whether the prerequisite is negated (using negation as failure)
443:             */
444:            public Prerequisite prereq(Predicate predicate, Object obj1,
445:                    Object obj2, boolean negatedAF) {
446:                Class[] structure = predicate.getStructure();
447:
448:                if (structure.length != 2) {
449:                    throw new IllegalArgumentException(
450:                            "Not the right number of terms for this predicate, the number is 2, but must be "
451:                                    + structure.length);
452:                }
453:
454:                Term[] terms = { toTerm(obj1, structure[0]),
455:                        toTerm(obj2, structure[1]) };
456:
457:                return lfactory.createPrerequisite(predicate, terms, negatedAF);
458:            }
459:
460:            /**
461:             * Create a prerequisite with three terms.
462:             * See <code>toTerm</code> how the object is converted to a term.
463:             * @return the prerequisite created
464:             * @param predicate the predicate
465:             * @param obj1 an object
466:             * @param obj2 an object
467:             * @param obj3 an object
468:             * @param negatedAF whether the prerequisite is negated (using negation as failure)
469:             */
470:            public Prerequisite prereq(Predicate predicate, Object obj1,
471:                    Object obj2, Object obj3, boolean negatedAF) {
472:                Class[] structure = predicate.getStructure();
473:
474:                if (structure.length != 3) {
475:                    throw new IllegalArgumentException(
476:                            "Not the right number of terms for this predicate, the number is 3, but must be "
477:                                    + structure.length);
478:                }
479:
480:                Term[] terms = { toTerm(obj1, structure[0]),
481:                        toTerm(obj2, structure[1]), toTerm(obj3, structure[2]) };
482:
483:                return lfactory.createPrerequisite(predicate, terms, negatedAF);
484:            }
485:
486:            /**
487:             * Create a prerequisite with four terms.
488:             * See <code>toTerm</code> how the object is converted to a term.
489:             * @return the prerequisite created
490:             * @param predicate the predicate
491:             * @param obj1 an object
492:             * @param obj2 an object
493:             * @param obj3 an object
494:             * @param obj4 an object
495:             * @param negatedAF whether the prerequisite is negated (using negation as failure)
496:             */
497:            public Prerequisite prereq(Predicate predicate, Object obj1,
498:                    Object obj2, Object obj3, Object obj4, boolean negatedAF) {
499:                Class[] structure = predicate.getStructure();
500:
501:                if (structure.length != 4) {
502:                    throw new IllegalArgumentException(
503:                            "Not the right number of terms for this predicate, the number is 4, but must be "
504:                                    + structure.length);
505:                }
506:
507:                Term[] terms = { toTerm(obj1, structure[0]),
508:                        toTerm(obj2, structure[1]), toTerm(obj3, structure[2]),
509:                        toTerm(obj4, structure[3]) };
510:
511:                return lfactory.createPrerequisite(predicate, terms, negatedAF);
512:            }
513:
514:            /**
515:             * Create a rule with two prerequisites.
516:             * The prerequisites are connected by OR.
517:             * @return the rule created
518:             * @param prerequisite1 a prerequisite
519:             * @param prerequisite2 a prerequisite
520:             * @param conclusion the concusion of the rule
521:             * @deprecated use Prerequisite instead of Fact to build rules
522:             */
523:            public Rule orRule(Fact prerequisite1, Fact prerequisite2,
524:                    Fact conclusion) {
525:                Vector body = new Vector();
526:                body.add(fact2prereq(prerequisite1));
527:                body.add(fact2prereq(prerequisite2));
528:                return lfactory.createRule(body, conclusion, true);
529:            }
530:
531:            /**
532:             * Create a rule with three prerequisites.
533:             * The prerequisites are connected by OR.
534:             * @return the rule created
535:             * @param prerequisite1 a prerequisite
536:             * @param prerequisite2 a prerequisite
537:             * @param prerequisite3 a prerequisite
538:             * @param conclusion the concusion of the rule
539:             * @deprecated use Prerequisite instead of Fact to build rules
540:             */
541:            public Rule orRule(Fact prerequisite1, Fact prerequisite2,
542:                    Fact prerequisite3, Fact conclusion) {
543:                Vector body = new Vector();
544:                body.add(fact2prereq(prerequisite1));
545:                body.add(fact2prereq(prerequisite2));
546:                body.add(fact2prereq(prerequisite3));
547:                return lfactory.createRule(body, conclusion, true);
548:            }
549:
550:            /**
551:             * Create a rule with four prerequisites.
552:             * The prerequisites are connected by OR.
553:             * @return the rule created
554:             * @param prerequisite1 a prerequisite
555:             * @param prerequisite2 a prerequisite
556:             * @param prerequisite3 a prerequisite
557:             * @param prerequisite4 a prerequisite
558:             * @param conclusion the concusion of the rule
559:             * @deprecated use Prerequisite instead of Fact to build rules
560:             */
561:            public Rule orRule(Fact prerequisite1, Fact prerequisite2,
562:                    Fact prerequisite3, Fact prerequisite4, Fact conclusion) {
563:                Vector body = new Vector();
564:                body.add(fact2prereq(prerequisite1));
565:                body.add(fact2prereq(prerequisite2));
566:                body.add(fact2prereq(prerequisite3));
567:                body.add(fact2prereq(prerequisite4));
568:                return lfactory.createRule(body, conclusion, true);
569:            }
570:
571:            /**
572:             * Create a rule.
573:             * @return the rule created
574:             * @param body the prerequisites of the rule
575:             * @param conclusion the concusion of the rule
576:             */
577:            public Rule rule(List body, Fact conclusion) {
578:                return lfactory.createRule(body, conclusion);
579:            }
580:
581:            /**
582:             * Create a rule with no prerequisites.
583:             * @return the rule created
584:             * @param conclusion the concusion of the rule
585:             */
586:            public Rule rule(Fact conclusion) {
587:                Vector body = new Vector();
588:                return lfactory.createRule(body, conclusion);
589:            }
590:
591:            /**
592:             * Create a rule with one prerequisite.
593:             * @return the rule created
594:             * @param prerequisite a prerequisite
595:             * @param conclusion the concusion of the rule
596:             * @deprecated use Prerequisite instead of Fact to build rules
597:             */
598:            public Rule rule(Fact prerequisite, Fact conclusion) {
599:                Vector body = new Vector();
600:                body.add(fact2prereq(prerequisite));
601:                return lfactory.createRule(body, conclusion);
602:            }
603:
604:            /**
605:             * Create a rule with two prerequisites.
606:             * @return the rule created
607:             * @param prerequisite1 a prerequisite
608:             * @param prerequisite2 a prerequisite
609:             * @param conclusion the concusion of the rule
610:             * @deprecated use Prerequisite instead of Fact to build rules
611:             */
612:            public Rule rule(Fact prerequisite1, Fact prerequisite2,
613:                    Fact conclusion) {
614:                Vector body = new Vector();
615:                body.add(fact2prereq(prerequisite1));
616:                body.add(fact2prereq(prerequisite2));
617:                return lfactory.createRule(body, conclusion);
618:            }
619:
620:            /**
621:             * Create a rule with three prerequisites.
622:             * @return the rule created
623:             * @param prerequisite1 a prerequisite
624:             * @param prerequisite2 a prerequisite
625:             * @param prerequisite3 a prerequisite
626:             * @param conclusion the concusion of the rule
627:             * @deprecated use Prerequisite instead of Fact to build rules
628:             */
629:            public Rule rule(Fact prerequisite1, Fact prerequisite2,
630:                    Fact prerequisite3, Fact conclusion) {
631:                Vector body = new Vector();
632:                body.add(fact2prereq(prerequisite1));
633:                body.add(fact2prereq(prerequisite2));
634:                body.add(fact2prereq(prerequisite3));
635:                return lfactory.createRule(body, conclusion);
636:            }
637:
638:            /**
639:             * Create a rule with four prerequisites.
640:             * @return the rule created
641:             * @param prerequisite1 a prerequisite
642:             * @param prerequisite2 a prerequisite
643:             * @param prerequisite3 a prerequisite
644:             * @param prerequisite4 a prerequisite
645:             * @param conclusion the concusion of the rule
646:             * @deprecated use Prerequisite instead of Fact to build rules
647:             */
648:            public Rule rule(Fact prerequisite1, Fact prerequisite2,
649:                    Fact prerequisite3, Fact prerequisite4, Fact conclusion) {
650:                Vector body = new Vector();
651:                body.add(fact2prereq(prerequisite1));
652:                body.add(fact2prereq(prerequisite2));
653:                body.add(fact2prereq(prerequisite3));
654:                body.add(fact2prereq(prerequisite4));
655:                return lfactory.createRule(body, conclusion);
656:            }
657:
658:            /**
659:             * Create a rule with two prerequisites.
660:             * The prerequisites are connected by OR.
661:             * @return the rule created
662:             * @param prerequisite1 a prerequisite
663:             * @param prerequisite2 a prerequisite
664:             * @param conclusion the concusion of the rule
665:             */
666:            public Rule orRule(Prerequisite prerequisite1,
667:                    Prerequisite prerequisite2, Fact conclusion) {
668:                Vector body = new Vector();
669:                body.add(prerequisite1);
670:                body.add(prerequisite2);
671:                return lfactory.createRule(body, conclusion, true);
672:            }
673:
674:            /**
675:             * Create a rule with three prerequisites.
676:             * The prerequisites are connected by OR.
677:             * @return the rule created
678:             * @param prerequisite1 a prerequisite
679:             * @param prerequisite2 a prerequisite
680:             * @param prerequisite3 a prerequisite
681:             * @param conclusion the concusion of the rule
682:             */
683:            public Rule orRule(Prerequisite prerequisite1,
684:                    Prerequisite prerequisite2, Prerequisite prerequisite3,
685:                    Fact conclusion) {
686:                Vector body = new Vector();
687:                body.add(prerequisite1);
688:                body.add(prerequisite2);
689:                body.add(prerequisite3);
690:                return lfactory.createRule(body, conclusion, true);
691:            }
692:
693:            /**
694:             * Create a rule with four prerequisites.
695:             * The prerequisites are connected by OR.
696:             * @return the rule created
697:             * @param prerequisite1 a prerequisite
698:             * @param prerequisite2 a prerequisite
699:             * @param prerequisite3 a prerequisite
700:             * @param prerequisite4 a prerequisite
701:             * @param conclusion the concusion of the rule
702:             */
703:            public Rule orRule(Prerequisite prerequisite1,
704:                    Prerequisite prerequisite2, Prerequisite prerequisite3,
705:                    Prerequisite prerequisite4, Fact conclusion) {
706:                Vector body = new Vector();
707:                body.add(prerequisite1);
708:                body.add(prerequisite2);
709:                body.add(prerequisite3);
710:                body.add(prerequisite4);
711:                return lfactory.createRule(body, conclusion, true);
712:            }
713:
714:            /**
715:             * Create a rule with one prerequisite.
716:             * @return the rule created
717:             * @param prerequisite a prerequisite
718:             * @param conclusion the concusion of the rule
719:             */
720:            public Rule rule(Prerequisite prerequisite, Fact conclusion) {
721:                Vector body = new Vector();
722:                body.add(prerequisite);
723:                return lfactory.createRule(body, conclusion);
724:            }
725:
726:            /**
727:             * Create a rule with two prerequisites.
728:             * @return the rule created
729:             * @param prerequisite1 a prerequisite
730:             * @param prerequisite2 a prerequisite
731:             * @param conclusion the concusion of the rule
732:             */
733:            public Rule rule(Prerequisite prerequisite1,
734:                    Prerequisite prerequisite2, Fact conclusion) {
735:                Vector body = new Vector();
736:                body.add(prerequisite1);
737:                body.add(prerequisite2);
738:                return lfactory.createRule(body, conclusion);
739:            }
740:
741:            /**
742:             * Create a rule with three prerequisites.
743:             * @return the rule created
744:             * @param prerequisite1 a prerequisite
745:             * @param prerequisite2 a prerequisite
746:             * @param prerequisite3 a prerequisite
747:             * @param conclusion the concusion of the rule
748:             */
749:            public Rule rule(Prerequisite prerequisite1,
750:                    Prerequisite prerequisite2, Prerequisite prerequisite3,
751:                    Fact conclusion) {
752:                Vector body = new Vector();
753:                body.add(prerequisite1);
754:                body.add(prerequisite2);
755:                body.add(prerequisite3);
756:                return lfactory.createRule(body, conclusion);
757:            }
758:
759:            /**
760:             * Create a rule with four prerequisites.
761:             * @return the rule created
762:             * @param prerequisite1 a prerequisite
763:             * @param prerequisite2 a prerequisite
764:             * @param prerequisite3 a prerequisite
765:             * @param prerequisite4 a prerequisite
766:             * @param conclusion the concusion of the rule
767:             */
768:            public Rule rule(Prerequisite prerequisite1,
769:                    Prerequisite prerequisite2, Prerequisite prerequisite3,
770:                    Prerequisite prerequisite4, Fact conclusion) {
771:                Vector body = new Vector();
772:                body.add(prerequisite1);
773:                body.add(prerequisite2);
774:                body.add(prerequisite3);
775:                body.add(prerequisite4);
776:                return lfactory.createRule(body, conclusion);
777:            }
778:
779:            /**
780:             * Convenient method to interprete objects as terms.
781:             * The following rules apply:
782:             * <ol>
783:             * <li> if the object is a term, keep it as it is
784:             * <li> if the object is an instance of clazz, create a constant term wrapping the object
785:             * <li> if the object is a string (an the previous does not apply), create a variable using the object as a name
786:             * <li> otherwise throw an illegal argument exception;
787:             * </ol>
788:             * @return a term
789:             * @param obj the object to be interpreted as a term
790:             * @param clazz the type of the term
791:             */
792:            private Term toTerm(Object obj, Class clazz) {
793:                if (obj == null) {
794:                    throw new IllegalArgumentException(
795:                            "Cannot build term from null");
796:                }
797:
798:                if (obj instanceof  Term) {
799:                    return (Term) obj;
800:                }
801:                // remark: this was a bug in 1.6 : if (clazz==obj.getClass())
802:                if (clazz.isAssignableFrom(obj.getClass())) {
803:                    return lfactory.createConstantTerm(obj);
804:                }
805:
806:                if (obj instanceof  String) {
807:                    return lfactory.createVariableTerm((String) obj, clazz);
808:                }
809:
810:                throw new IllegalArgumentException("Cannot build term from "
811:                        + obj);
812:            }
813:
814:            /**
815:             * Convenient method to create a variable.
816:             * @return a term
817:             * @param name a var name
818:             * @param clazz a type
819:             */
820:            public Term variable(String name, Class clazz) {
821:                return lfactory.createVariableTerm(name, clazz);
822:            }
823:
824:            /**
825:             * Convenient method to create a string type variable.
826:             * @return a term
827:             * @param name a var name
828:             */
829:            public Term variable(String name) {
830:                return variable(name, String.class);
831:            }
832:
833:            /**
834:             * Create a query with one fact.
835:             * @return the query created
836:             * @param fact a fact
837:             * @param name the name of the query
838:             */
839:            public Query query(Fact fact, String name) {
840:                Fact[] facts = { fact };
841:                return query(facts, name);
842:            }
843:
844:            /**
845:             * Create a query with two facts.
846:             * @return the query created
847:             * @param fact1 a fact
848:             * @param fact2 a fact
849:             * @param name the name of the query
850:             */
851:            public Query query(Fact fact1, Fact fact2, String name) {
852:                Fact[] facts = { fact1, fact2 };
853:                return query(facts, name);
854:            }
855:
856:            /**
857:             * Create a query with three facts.
858:             * @return the query created
859:             * @param fact1 a fact
860:             * @param fact2 a fact
861:             * @param fact3 a fact
862:             * @param name the name of the query
863:             */
864:            public Query query(Fact fact1, Fact fact2, Fact fact3, String name) {
865:                Fact[] facts = { fact1, fact2, fact3 };
866:                return query(facts, name);
867:            }
868:
869:            /**
870:             * Create a query with N facts.
871:             * @return the query created
872:             * @param fact1 a fact
873:             * @param fact2 a fact
874:             * @param name the name of the query
875:             */
876:            public Query query(Fact[] facts, String name) {
877:                return lfactory.createQuery(facts, name);
878:            }
879:
880:            /** 
881:             * Convert a fact to a prerequisite.
882:             * @param fact a fact
883:             * @return a prerequisite
884:             */
885:            public Prerequisite fact2prereq(Fact fact) {
886:                Predicate p = fact.getPredicate();
887:                Term[] terms = fact.getTerms();
888:                return this .prereq(p, terms);
889:            }
890:
891:            /**
892:             * Create a prerequiste using the cut predicate.
893:             * @return a prerequisite
894:             */
895:            public Prerequisite cut() {
896:                return lfactory.createCut();
897:            }
898:
899:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.