001: package org.drools.eclipse;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import org.drools.compiler.Dialect;
009: import org.drools.compiler.DroolsError;
010: import org.drools.lang.descr.FunctionDescr;
011: import org.drools.lang.descr.PackageDescr;
012: import org.drools.lang.descr.RuleDescr;
013: import org.drools.rule.LineMappings;
014: import org.drools.rule.Package;
015: import org.drools.rule.PackageCompilationData;
016:
017: public class DRLInfo {
018:
019: private static final DroolsError[] EMPTY_DROOLS_ERROR_ARRAY = new DroolsError[0];
020: private static final List EMPTY_LIST = Collections
021: .unmodifiableList(Collections.EMPTY_LIST);
022:
023: private String sourcePathName;
024: private PackageDescr packageDescr;
025: private List parserErrors;
026: private Package compiledPackage;
027: private DroolsError[] builderErrors;
028: // cached entry
029: private transient RuleInfo[] ruleInfos;
030: private transient FunctionInfo[] functionInfos;
031:
032: public DRLInfo(String sourcePathName, PackageDescr packageDescr,
033: List parserErrors) {
034: if (sourcePathName == null || "".equals(sourcePathName)) {
035: throw new IllegalArgumentException(
036: "Invalid sourcePathName " + sourcePathName);
037: }
038: if (packageDescr == null) {
039: throw new IllegalArgumentException("Null packageDescr");
040: }
041: this .sourcePathName = sourcePathName;
042: this .packageDescr = packageDescr;
043: this .parserErrors = parserErrors == null ? EMPTY_LIST
044: : Collections.unmodifiableList(parserErrors);
045: this .builderErrors = EMPTY_DROOLS_ERROR_ARRAY;
046: }
047:
048: public DRLInfo(String pathName, PackageDescr packageDescr,
049: List parserErrors, Package compiledPackage,
050: DroolsError[] builderErrors) {
051: this (pathName, packageDescr, parserErrors);
052: if (compiledPackage == null) {
053: throw new IllegalArgumentException("Null package");
054: }
055: this .compiledPackage = compiledPackage;
056: this .builderErrors = builderErrors == null ? EMPTY_DROOLS_ERROR_ARRAY
057: : builderErrors;
058: }
059:
060: public String getSourcePathName() {
061: return sourcePathName;
062: }
063:
064: public PackageDescr getPackageDescr() {
065: return packageDescr;
066: }
067:
068: public List getParserErrors() {
069: return parserErrors;
070: }
071:
072: public Package getPackage() {
073: return compiledPackage;
074: }
075:
076: public DroolsError[] getBuilderErrors() {
077: return builderErrors;
078: }
079:
080: public String getPackageName() {
081: return packageDescr.getName();
082: }
083:
084: public boolean isCompiled() {
085: return compiledPackage != null;
086: }
087:
088: public RuleInfo[] getRuleInfos() {
089: if (ruleInfos == null) {
090: List ruleInfosList = new ArrayList();
091: for (Iterator rules = packageDescr.getRules().iterator(); rules
092: .hasNext();) {
093: RuleDescr ruleDescr = (RuleDescr) rules.next();
094: RuleInfo ruleInfo = new RuleInfo(ruleDescr);
095: ruleInfosList.add(ruleInfo);
096: }
097: ruleInfos = (RuleInfo[]) ruleInfosList
098: .toArray(new RuleInfo[0]);
099: }
100: return ruleInfos;
101: }
102:
103: public RuleInfo getRuleInfo(int drlLineNumber) {
104: RuleInfo[] ruleInfos = getRuleInfos();
105:
106: int ruleLine = -1;
107: RuleInfo result = null;
108: for (int i = 0; i < ruleInfos.length; i++) {
109: int ruleDrlLineNumber = ruleInfos[i].getDrlLineNumber();
110: if (ruleDrlLineNumber > ruleLine
111: && ruleDrlLineNumber <= drlLineNumber + 1) {
112: ruleLine = ruleDrlLineNumber;
113: result = ruleInfos[i];
114: }
115: }
116: return result;
117: }
118:
119: public class RuleInfo {
120:
121: private final RuleDescr ruleDescr;
122: // cached entries
123: private transient String className;
124: private transient int consequenceJavaLineNumber = -1;
125:
126: public RuleInfo(RuleDescr ruleDescr) {
127: if (ruleDescr == null) {
128: throw new IllegalArgumentException("Null ruleDescr");
129: }
130: this .ruleDescr = ruleDescr;
131: }
132:
133: public Dialect getDialect() {
134: return ruleDescr.getDialect();
135: }
136:
137: public String getSourcePathName() {
138: return DRLInfo.this .getSourcePathName();
139: }
140:
141: public String getClassName() {
142: // ruleDescr is only filled in during compilation
143: if (!isCompiled()) {
144: throw new IllegalArgumentException(
145: "Package has not been compiled");
146: }
147: if (className == null) {
148: className = getPackageName() + "."
149: + ruleDescr.getClassName();
150: }
151: return className;
152: }
153:
154: public int getDrlLineNumber() {
155: return ruleDescr.getLine();
156: }
157:
158: public int getConsequenceDrlLineNumber() {
159: return ruleDescr.getConsequenceLine();
160: }
161:
162: public int getConsequenceJavaLineNumber() {
163: if (consequenceJavaLineNumber == -1) {
164: if (!isCompiled()) {
165: throw new IllegalArgumentException(
166: "Package has not been compiled");
167: }
168: PackageCompilationData data = compiledPackage
169: .getPackageCompilationData();
170:
171: LineMappings mappings = data.getLineMappings(className);
172: consequenceJavaLineNumber = mappings.getOffset();
173:
174: }
175: return consequenceJavaLineNumber;
176: }
177:
178: public String getPackageName() {
179: return packageDescr.getName();
180: }
181:
182: public String getRuleName() {
183: return ruleDescr.getName();
184: }
185: }
186:
187: public FunctionInfo[] getFunctionInfos() {
188: if (functionInfos == null) {
189: List functionInfosList = new ArrayList();
190: for (Iterator functions = packageDescr.getFunctions()
191: .iterator(); functions.hasNext();) {
192: FunctionDescr functionDescr = (FunctionDescr) functions
193: .next();
194: FunctionInfo functionInfo = new FunctionInfo(
195: functionDescr);
196: functionInfosList.add(functionInfo);
197: }
198: functionInfos = (FunctionInfo[]) functionInfosList
199: .toArray(new FunctionInfo[0]);
200: }
201: return functionInfos;
202: }
203:
204: public FunctionInfo getFunctionInfo(int drlLineNumber) {
205: FunctionInfo[] functionInfos = getFunctionInfos();
206: int functionLine = -1;
207: FunctionInfo result = null;
208: for (int i = 0; i < functionInfos.length; i++) {
209: int functionDrlLineNumber = functionInfos[i]
210: .getDrlLineNumber();
211: if (functionDrlLineNumber > functionLine
212: && functionDrlLineNumber <= drlLineNumber + 1) {
213: functionLine = functionDrlLineNumber;
214: result = functionInfos[i];
215: }
216: }
217: return result;
218: }
219:
220: public class FunctionInfo {
221:
222: private FunctionDescr functionDescr;
223: // cached entries
224: private transient String className;
225: private transient int javaLineNumber = -1;
226:
227: public FunctionInfo(FunctionDescr functionDescr) {
228: if (functionDescr == null) {
229: throw new IllegalArgumentException("Null functionDescr");
230: }
231: this .functionDescr = functionDescr;
232: }
233:
234: public String getSourcePathName() {
235: return DRLInfo.this .getSourcePathName();
236: }
237:
238: public String getClassName() {
239: // functionDescr is only filled in during compilation
240: if (!isCompiled()) {
241: throw new IllegalArgumentException(
242: "Package has not been compiled");
243: }
244: if (className == null) {
245: className = functionDescr.getClassName();
246: }
247: return className;
248: }
249:
250: public int getDrlLineNumber() {
251: return functionDescr.getLine();
252: }
253:
254: public int getJavaLineNumber() {
255: if (javaLineNumber == -1) {
256: if (!isCompiled()) {
257: throw new IllegalArgumentException(
258: "Package has not been compiled");
259: }
260: javaLineNumber = compiledPackage
261: .getPackageCompilationData().getLineMappings(
262: className).getOffset();
263: }
264: return javaLineNumber;
265: }
266:
267: public String getPackageName() {
268: return packageDescr.getName();
269: }
270:
271: public String getFunctionName() {
272: return functionDescr.getName();
273: }
274: }
275:
276: }
|