@Override public Collection<RuleBaseElementId> list(RuleBaseElementType type) { RuleBaseElementId example = new RuleBaseElementId(); example.setPackageName(null); example.setType(type); return listByExample(example); }
public RuleBaseElement(RuleBaseElementId id) { name = id.getName(); packageName = id.getPackageName(); type = id.getType(); }
private RuleBaseElementId getIdforFile(File artifact) throws RuleBaseException, SAXException, IOException, ParserConfigurationException { RuleBaseElementType type = getTypeFromFile(artifact); String name = FilenameUtils.removeExtension(artifact.getName()); RuleBaseElementId id = new RuleBaseElementId(type, name); if (artifact.exists() && type.equals(RuleBaseElementType.Process)) { id.setPackageName(readPackageNameFromProcessFile(artifact)); } return id; }
private void installRuleBaseElement(File artifact) throws RuleBaseException, IOException, SAXException, ParserConfigurationException { RuleBaseElementId id = getIdforFile(artifact); String code = FileUtils.readFileToString(artifact); ruleManager.addOrUpdate(id, code); if (id.getType().equals(RuleBaseElementType.Process)) { cache.put(artifact.getName(), id); } LOGGER.info("Successfully installed workflow file \"{}\"", artifact.getName()); }
@Override public void update(File artifact) throws Exception { LOGGER.debug("WorkflowDeployer.update(\"{}\")", artifact.getAbsolutePath()); try { RuleBaseElementId id = getIdforFile(artifact); String code = FileUtils.readFileToString(artifact); boolean changed = false; if (id.getType().equals(RuleBaseElementType.Process)) { RuleBaseElementId cachedId = cache.get(artifact.getName()); if (!id.equals(cachedId)) { ruleManager.delete(cachedId); changed = true; } } ruleManager.addOrUpdate(id, code); if (changed) { cache.put(artifact.getName(), id); } } catch (Exception e) { LOGGER.error(e.getMessage()); throw e; } LOGGER.info("Successfully updated workflow file \"{}\"", artifact.getName()); }
String packageName = id.getPackageName(); StringBuffer packageString = getPackageString(packageName); String code = manager.get(id); String packageName = id.getPackageName(); StringBuffer packageString = getPackageString(packageName); String code = manager.get(id); String formattedRule = String.format(RULE_TEMPLATE, id.getName(), code); packageString.append(formattedRule);
public RuleBaseElementId generateId() { return new RuleBaseElementId(type, packageName, name); }
@Override public boolean apply(RuleBaseElementId input) { return input.getPackageName().equals(packageName); } });
public synchronized void reloadPackage(String packageName) throws RuleBaseException { long start = System.currentTimeMillis(); packageStrings.clear(); StringBuffer packageString = initNewPackageString(packageName); for (RuleBaseElementId id : manager.list(RuleBaseElementType.Function, packageName)) { String code = manager.get(id); packageString.append(code); } for (RuleBaseElementId id : manager.list(RuleBaseElementType.Rule, packageName)) { String code = manager.get(id); String formattedRule = String.format(RULE_TEMPLATE, id.getName(), code); packageString.append(formattedRule); } Collection<String> flows = queryFlows(packageName); Collection<KnowledgePackage> compiledPackage = compileDrlString(packageString.toString(), flows); if (base.getKnowledgePackage(packageName) != null) { base.removeKnowledgePackage(packageName); } base.addKnowledgePackages(compiledPackage); LOGGER.info("Reloading only package {} took {}ms", packageName, System.currentTimeMillis() - start); }
@Override public String get(RuleBaseElementId name) { return getRessourceHandler(name.getType()).get(name); }
private String getUniqueRuleName(String name) { Collection<RuleBaseElementId> list = ruleManager.list(RuleBaseElementType.Rule); while (list.contains(new RuleBaseElementId(RuleBaseElementType.Rule, name))) { name = name + "_"; } return name; }
@Override public void delete(RuleBaseElementId name) throws RuleBaseException { try { persistence.delete(new RuleBaseElement(name)); } catch (PersistenceException e) { throw new RuleBaseException(e); } builder.reloadPackage(name.getPackageName()); }
@Override public Collection<RuleBaseElementId> list(RuleBaseElementType type, String packageName) { RuleBaseElementId example = new RuleBaseElementId(); example.setType(type); example.setPackageName(packageName); return listByExample(example); }
@Override public void update(RuleBaseElementId name, String newCode) throws RuleBaseException { getRessourceHandler(name.getType()).update(name, newCode); }
@Override public void registerFlowTriggerEvent(Event event, String... flowIds) throws WorkflowException { String eventName = event.getName(); String ruleName = String.format("_generated_ trigger %s on %s", Arrays.asList(flowIds), eventName); StringBuffer ruleCode = generateFlowTriggerRule(event, flowIds); LOGGER.info("adding new rule with id: {}", ruleName); try { rulemanager.add(new RuleBaseElementId(RuleBaseElementType.Rule, ruleName), ruleCode.toString()); } catch (RuleBaseException e) { throw new WorkflowException(e); } }
@Override public void update(RuleBaseElementId name, String newCode) throws RuleBaseException { RuleBaseElement oldBean = new RuleBaseElement(name); RuleBaseElement newBean = new RuleBaseElement(name, newCode); try { persistence.update(oldBean, newBean); } catch (PersistenceException e) { throw new RuleBaseException(e); } builder.reloadPackage(name.getPackageName()); }
@Override public void delete(RuleBaseElementId name) throws RuleBaseException { getRessourceHandler(name.getType()).delete(name); }
@Override public synchronized void registerEvent(RemoteEvent event, String portId, String returnAddress, String serviceId) { String name = String.format("Notify %s via %s when %s occurs", returnAddress.toString(), portId, event.getClassName()); name = getUniqueRuleName(name); RuleBaseElementId id = new RuleBaseElementId(RuleBaseElementType.Rule, name); String eventMatcher = makeEventMatcher(event); try { String osgiHelperStatement; if (serviceId == null) { osgiHelperStatement = String.format(OSGI_HELPER_TEMPLATE1, portId, returnAddress); } else { osgiHelperStatement = String.format(OSGI_HELPER_TEMPLATE2, portId, returnAddress, serviceId); } ruleManager.add(id, String.format(EVENT_REGISTRATION_RULE_TEMPLATE, eventMatcher, osgiHelperStatement)); } catch (RuleBaseException e) { throw new IllegalArgumentException(e); } LOGGER.info("registering Event: {}", event); }
@Override public void add(RuleBaseElementId name, String code) throws RuleBaseException { List<RuleBaseElement> existingRules = persistence.query(new RuleBaseElement(name)); if (!existingRules.isEmpty()) { throw new RuleBaseException("rule already exists"); } RuleBaseElement objectToPersist = new RuleBaseElement(name, code); try { persistence.create(objectToPersist); } catch (PersistenceException e) { throw new RuleBaseException(e); } try { builder.reloadPackage(name.getPackageName()); } catch (RuleBaseException e) { try { persistence.delete(objectToPersist); throw e; } catch (PersistenceException e1) { throw new RuntimeException("could not remove previously added rule, that broke the rulebase", e1); } } }
@Override public void add(RuleBaseElementId name, String code) throws RuleBaseException { getRessourceHandler(name.getType()).create(name, code); }