⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rulecompletionprocessor.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			    	break;
			}
		}
	}
	
	private String getPropertyClass(String className, String propertyName) {
		if (className != null) {
			ClassTypeResolver resolver = new ClassTypeResolver(getDRLEditor().getImports(), ProjectClassLoader.getProjectClassLoader(getEditor()));
			try {
				Class clazz = resolver.resolveType(className);
				if (clazz != null) {
					Class clazzz = (Class) new ClassFieldInspector(clazz).getFieldTypes().get(propertyName);
					if (clazzz != null) {
						return clazzz.getName();
					}
				}
			} catch (IOException exc) {
				// Do nothing
			} catch (ClassNotFoundException exc) {
				// Do nothing
			}
		}
		return null;
	}
	
	private boolean isComparable(String type) {
		if (type == null) {
			return false;
		}
		if (isPrimitiveNumericType(type)) {
			return true;
		}
		if (isSubtypeOf(type, "java.lang.Comparable")) {
			return true;
		}
		return false;
	}
	
	private boolean isPrimitiveType(String type) {
		return isPrimitiveNumericType(type)
			|| type.equals("boolean");
	}
	
	private boolean isPrimitiveNumericType(String type) {
		return type.equals("byte") || type.equals("short")
				|| type.equals("int") || type.equals("long")
				|| type.equals("float") || type.equals("double")
				|| type.equals("char");
	}
	
	/**
	 * Returns true if the first class is the same or a subtype of the second class.
	 * @param class1
	 * @param class2
	 * @return
	 */
	private boolean isSubtypeOf(String class1, String class2) {
		if (class1 == null || class2 == null) {
			return false;
		}
		class1 = convertToNonPrimitiveClass(class1);
		class2 = convertToNonPrimitiveClass(class2);
		// TODO add code to take primitive types into account
		ClassTypeResolver resolver = new ClassTypeResolver(getDRLEditor().getImports(), ProjectClassLoader.getProjectClassLoader(getEditor()));
		try {
			Class clazz1 = resolver.resolveType(class1);
			Class clazz2 = resolver.resolveType(class2);
			if (clazz1 == null || clazz2 == null) {
				return false;
			}
			return clazz2.isAssignableFrom(clazz1);
		} catch (ClassNotFoundException exc) {
			return false;
		}
	}
	
	private String convertToNonPrimitiveClass(String clazz) {
		if (!isPrimitiveType(clazz)) {
			return clazz;
		}
		if ("byte".equals(clazz)) {
			return "java.lang.Byte";
		} else if ("short".equals(clazz)) {
			return "java.lang.Short";
		} else if ("int".equals(clazz)) {
			return "java.lang.Integer";
		} else if ("long".equals(clazz)) {
			return "java.lang.Long";
		} else if ("float".equals(clazz)) {
			return "java.lang.Float";
		} else if ("double".equals(clazz)) {
			return "java.lang.Double";
		} else if ("char".equals(clazz)) {
			return "java.lang.Char";
		} else if ("boolean".equals(clazz)) {
			return "java.lang.Boolean";
		}
		// should never occur
		return null;
	}
	
	private boolean consequence(String backText) {
		return isKeywordOnLine(backText, "then");
	}

	private boolean condition(String backText) {
		return isKeywordOnLine(backText, "when");
	}

	boolean query(String backText) {
		return query.matcher(backText).matches();
	}
	
	/**
	 * Check to see if the keyword appears on a line by itself.
	 */
	private boolean isKeywordOnLine(String chunk, String keyword) {
		StringTokenizer st = new StringTokenizer(chunk, "\n\t");
    	while(st.hasMoreTokens()) {
    		if (st.nextToken().trim().equals(keyword)) {
    			return true;
    		}    		
    	}
    	return false;
	}

    private void addRHSFunctionCompletionProposals(ITextViewer viewer,
                                                   final List list,
                                                   final String prefix) throws CoreException,
                                                                       DroolsParserException {
        Iterator iterator;
        RuleCompletionProposal prop;
        List functions = getDRLEditor().getFunctions();
        iterator = functions.iterator();
        while (iterator.hasNext()) {
            String name = (String) iterator.next() + "()";
        	prop = new RuleCompletionProposal(prefix.length(), name, name + ";", name.length() - 1);
        	prop.setPriority(-1);
        	prop.setImage(methodIcon);
        	list.add(prop);
        }
    }

    private void addRHSCompletionProposals(final List list,
                                           final String prefix) {
        RuleCompletionProposal prop = new RuleCompletionProposal(prefix.length(), "modify", "modify();", 7);
        prop.setImage(droolsIcon);
        list.add(prop);
        prop = new RuleCompletionProposal(prefix.length(), "retract", "retract();", 8);
        prop.setImage(droolsIcon);
        list.add(prop);
        prop = new RuleCompletionProposal(prefix.length(), "assert", "assert();", 7);
        prop.setImage(droolsIcon);
        list.add(prop);
        prop = new RuleCompletionProposal(prefix.length(), "assertLogical", "assertLogical();", 14);
        prop.setImage(droolsIcon);
        list.add(prop);
    }
    
    private void addRHSJavaCompletionProposals(List list, String backText, String prefix) {
    	int thenPosition = backText.lastIndexOf("then");
    	String conditions = backText.substring(0, thenPosition);
		Map params = new HashMap();
    	DrlParser parser = new DrlParser();
    	try {
    		PackageDescr descr = parser.parse(conditions);
    		List rules = descr.getRules();
    		if (rules != null && rules.size() == 1) {
    			getRuleParameters(params, ((RuleDescr) rules.get(0)).getLhs().getDescrs());
    			// rule params are already added by JavaCompletionProposals
    			// 
    			// Iterator iterator = params.keySet().iterator();
    			// while (iterator.hasNext()) {
    			// 	String name = (String) iterator.next();
    			// 	RuleCompletionProposal prop = new RuleCompletionProposal(prefix.length(), name, name + ".");
				// 	prop.setPriority(-1);
				// 	prop.setImage(methodIcon);
				// 	list.add(prop);
    			// }
    		}
    	} catch (DroolsParserException exc) {
    		// do nothing
    	}
    	String consequence = backText.substring(thenPosition + 4);
    	list.addAll(getJavaCompletionProposals(consequence, prefix, params));
    }
    
    private void getRuleParameters(Map result, List descrs) {
    	if (descrs == null) {
    		return;
    	}
    	Iterator iterator = descrs.iterator();
    	while (iterator.hasNext()) {
    		PatternDescr descr = (PatternDescr) iterator.next();
    		if (descr instanceof ColumnDescr) {
				String name = ((ColumnDescr) descr).getIdentifier();
				if (name != null) {
					result.put(name, ((ColumnDescr) descr).getObjectType());
				}
				getRuleSubParameters(result, ((ColumnDescr) descr).getDescrs(), ((ColumnDescr) descr).getObjectType());
			} else if (descr instanceof AndDescr) {
				getRuleParameters(result, ((AndDescr) descr).getDescrs());
			} else if (descr instanceof OrDescr) {
				getRuleParameters(result, ((OrDescr) descr).getDescrs());
			} else if (descr instanceof ExistsDescr) {
				getRuleParameters(result, ((ExistsDescr) descr).getDescrs());
			} else if (descr instanceof NotDescr) {
				getRuleParameters(result, ((NotDescr) descr).getDescrs());
			}
		}
    }
    
    private void getRuleSubParameters(Map result, List descrs, String clazz) {
    	if (descrs == null) {
    		return;
    	}
    	Iterator iterator = descrs.iterator();
    	while (iterator.hasNext()) {
    		PatternDescr descr = (PatternDescr) iterator.next();
    		if (descr instanceof FieldBindingDescr) {
				FieldBindingDescr fieldDescr = (FieldBindingDescr) descr;
				String name = fieldDescr.getIdentifier();
				String field = fieldDescr.getFieldName();
				String type = getPropertyClass(clazz, field);
				if (name != null) {
					result.put(name, type);
				}
			}
    	}
    }

    private void addRuleHeaderItems(final List list,
                                    final String prefix) {
        list.add(new RuleCompletionProposal(prefix.length(), "salience", "salience ", droolsIcon));
        list.add(new RuleCompletionProposal(prefix.length(), "no-loop", "no-loop ", droolsIcon));
        list.add(new RuleCompletionProposal(prefix.length(), "agenda-group", "agenda-group ", droolsIcon));
        list.add(new RuleCompletionProposal(prefix.length(), "duration", "duration ", droolsIcon));           
        list.add(new RuleCompletionProposal(prefix.length(), "auto-focus", "auto-focus ", droolsIcon));           
        list.add(new RuleCompletionProposal(prefix.length(), "when", "when" + System.getProperty("line.separator") + "\t ", droolsIcon));
        list.add(new RuleCompletionProposal(prefix.length(), "activation-group", "activation-group ", droolsIcon));        
    }

    private void addDSLProposals(final List list,
                                 final String prefix,
                                 List dslItems) {
        Iterator iterator = dslItems.iterator();
        while (iterator.hasNext()) {
        	String consequence = (String) iterator.next();
            RuleCompletionProposal p = new RuleCompletionProposal(prefix.length(), consequence);
            p.setImage( dslIcon );
            list.add(p);
        }
    }

    /** 
     * Lazily get the adapter for DSLs, and cache it with the editor for future reference.
     * If it is unable to load a DSL, it will try again next time.
     * But once it has found and loaded one, it will keep it until the editor is closed.
     * 
     * This delegates to DSLAdapter to poke around the project to try and load the DSL.
     */
    private DSLAdapter getDSLAdapter(ITextViewer viewer) {
    	// TODO: cache DSL adapter in plugin, and reset when dsl file saved
    	// retrieve dsl name always (might have changed) and try retrieving
    	// cached dsl from plugin first
//    	return new DSLAdapter(viewer.getDocument().get(), ((FileEditorInput) getEditor().getEditorInput()).getFile());
        DSLAdapter adapter = getDRLEditor().getDSLAdapter();
        if (adapter == null) {
            String content = viewer.getDocument().get();
            adapter = new DSLAdapter(content, ((FileEditorInput) getEditor().getEditorInput()).getFile());
            if (adapter.isValid()) {
            	getDRLEditor().setDSLAdapter(adapter);
            }
        }
        return adapter;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -