SoftConstraintMatchPersistenceExtension implements RuleModelIActionPersistenceExtension { @Override public PluggableIAction unmarshal(final String iActionString) throws RuleModelDRLPersistenceException { String[] parameters = PersistenceExtensionUtils.unwrapParenthesis(iActionString).split("\\s*,\\s*"); if ("kcontext".equals(parameters[0])) { if (parameters.length == 2) { return new ActionSoftConstraintMatch(PersistenceExtensionUtils.extractConstraintMatchValue(parameters[1])); } if (parameters.length == 3) { try { int bendableScoreLevel = Integer.parseInt(parameters[1]); return new ActionBendableSoftConstraintMatch(bendableScoreLevel, PersistenceExtensionUtils.extractConstraintMatchValue(parameters[2])); } catch (NumberFormatException e) { LOGGER.error("Could not parse bendable score level parameter " + parameters[1] + " as an Integer"); } } } throw new RuleModelDRLPersistenceException(PersistenceExtensionUtils.EXCEPTION_MESSAGE_BASE + iActionString); }  @Override boolean accept(final String iActionString); @Override PluggableIAction unmarshal(final String iActionString);  }
@Test public void unmarshalSoftConstraintMatch() throws RuleModelDRLPersistenceException { String actionString = "scoreHolder.addSoftConstraintMatch(kcontext, -1);"; IAction action = extension.unmarshal(actionString); assertTrue(action instanceof ActionSoftConstraintMatch); ActionSoftConstraintMatch actionSoftConstraintMatch = (ActionSoftConstraintMatch) action; assertEquals("-1", actionSoftConstraintMatch.getConstraintMatch()); }
@Test public void unmarshalSoftConstraintMatchNull() throws RuleModelDRLPersistenceException { String actionString = "scoreHolder.addSoftConstraintMatch(kcontext, null);"; IAction action = extension.unmarshal(actionString); assertTrue(action instanceof ActionSoftConstraintMatch); ActionSoftConstraintMatch actionSoftConstraintMatch = (ActionSoftConstraintMatch) action; assertNull(actionSoftConstraintMatch.getConstraintMatch()); }
@Test public void unmarshalActionBendableSoftConstraintMatch() throws RuleModelDRLPersistenceException { String actionString = "scoreHolder.addSoftConstraintMatch(kcontext, 1, -1);"; IAction action = extension.unmarshal(actionString); assertTrue(action instanceof ActionBendableSoftConstraintMatch); ActionBendableSoftConstraintMatch actionSoftConstraintMatch = (ActionBendableSoftConstraintMatch) action; assertEquals(1, actionSoftConstraintMatch.getPosition()); assertEquals("-1", actionSoftConstraintMatch.getConstraintMatch()); }