001: package org.drools.eclipse.core;
002:
003: import java.util.Iterator;
004: import java.util.Map;
005:
006: import org.eclipse.core.resources.IFile;
007:
008: public class DroolsModelBuilder {
009:
010: public static RuleSet createRuleSet() {
011: return new RuleSet();
012: }
013:
014: public static Package createPackage(String packageName, int offset,
015: int length) {
016: Package pkg = new Package(null, packageName);
017: pkg.setFile(null, offset, length);
018: return pkg;
019: }
020:
021: public static Package addPackage(RuleSet ruleSet,
022: String packageName, int offset, int length) {
023: Package pkg = new Package(ruleSet, packageName);
024: pkg.setFile(null, offset, length);
025: ruleSet.addPackage(pkg);
026: return pkg;
027: }
028:
029: public static void removePackage(Package pkg) {
030: RuleSet ruleSet = pkg.getParentRuleSet();
031: if (ruleSet != null) {
032: ruleSet.removePackage(pkg.getPackageName());
033: }
034: }
035:
036: public static void clearRuleSet(RuleSet ruleSet) {
037: ruleSet.clear();
038: }
039:
040: public static Rule addRule(Package pkg, String ruleName,
041: IFile file, int offset, int length, Map attributes) {
042: Rule rule = new Rule(pkg, ruleName);
043: rule.setFile(file, offset, length);
044: if (attributes != null) {
045: for (Iterator iterator = attributes.entrySet().iterator(); iterator
046: .hasNext();) {
047: Map.Entry entry = (Map.Entry) iterator.next();
048: RuleAttribute attribute = new RuleAttribute(rule,
049: (String) entry.getKey(), entry.getValue());
050: attribute.setFile(file, offset, length);
051: rule.addAttribute(attribute);
052: }
053: }
054: pkg.addRule(rule);
055: return rule;
056: }
057:
058: public static void removeRule(Rule rule) {
059: Package pkg = rule.getParentPackage();
060: if (pkg != null) {
061: pkg.removeRule(rule);
062: if (pkg.getChildren().length == 0) {
063: removePackage(pkg);
064: }
065: }
066: }
067:
068: public static Function addFunction(Package pkg,
069: String functionName, IFile file, int offset, int length) {
070: Function function = new Function(pkg, functionName);
071: function.setFile(file, offset, length);
072: pkg.addFunction(function);
073: return function;
074: }
075:
076: public static void removeFunction(Function function) {
077: Package pkg = function.getParentPackage();
078: if (pkg != null) {
079: pkg.removeFunction(function);
080: }
081: }
082:
083: public static void addExpander(Package pkg, String expanderName,
084: IFile file, int offset, int length) {
085: Expander expander = new Expander(pkg, expanderName);
086: expander.setFile(file, offset, length);
087: pkg.addExpander(expander);
088: }
089:
090: public static void removeExpander(Expander expander) {
091: Package pkg = expander.getParentPackage();
092: if (pkg != null) {
093: pkg.removeExpander(expander);
094: }
095: }
096:
097: public static void addImport(Package pkg, String importClass,
098: IFile file, int offset, int length) {
099: Import imp = new Import(pkg, importClass);
100: imp.setFile(file, offset, length);
101: pkg.addImport(imp);
102: }
103:
104: public static void removeImport(Import imp) {
105: Package pkg = imp.getParentPackage();
106: if (pkg != null) {
107: pkg.removeImport(imp);
108: }
109: }
110:
111: public static Global addGlobal(Package pkg, String globalName,
112: IFile file, int offset, int length) {
113: Global global = new Global(pkg, globalName);
114: global.setFile(file, offset, length);
115: pkg.addGlobal(global);
116: return global;
117: }
118:
119: public static void removeGlobal(Global global) {
120: Package pkg = global.getParentPackage();
121: if (pkg != null) {
122: pkg.removeGlobal(global);
123: }
124: }
125:
126: public static Query addQuery(Package pkg, String queryName,
127: IFile file, int offset, int length) {
128: Query query = new Query(pkg, queryName);
129: query.setFile(file, offset, length);
130: pkg.addQuery(query);
131: return query;
132: }
133:
134: public static void removeQuery(Query query) {
135: Package pkg = query.getParentPackage();
136: if (pkg != null) {
137: pkg.removeQuery(query);
138: }
139: }
140:
141: public static Template addTemplate(Package pkg,
142: String templateName, IFile file, int offset, int length) {
143: Template template = new Template(pkg, templateName);
144: template.setFile(file, offset, length);
145: pkg.addTemplate(template);
146: return template;
147: }
148:
149: public static void removeTemplate(Template template) {
150: Package pkg = template.getParentPackage();
151: if (pkg != null) {
152: pkg.removeTemplate(template);
153: }
154: }
155:
156: public static void removeElement(DroolsElement element) {
157: switch (element.getType()) {
158: case DroolsElement.RULESET:
159: clearRuleSet((RuleSet) element);
160: break;
161: case DroolsElement.PACKAGE:
162: removePackage((Package) element);
163: break;
164: case DroolsElement.RULE:
165: removeRule((Rule) element);
166: break;
167: case DroolsElement.QUERY:
168: removeQuery((Query) element);
169: break;
170: case DroolsElement.FUNCTION:
171: removeFunction((Function) element);
172: break;
173: case DroolsElement.TEMPLATE:
174: removeTemplate((Template) element);
175: break;
176: case DroolsElement.EXPANDER:
177: removeExpander((Expander) element);
178: break;
179: case DroolsElement.GLOBAL:
180: removeGlobal((Global) element);
181: break;
182: }
183: }
184:
185: }
|