Skip to content

Commit

Permalink
DROOLS-4425: Refactor drools-workbench-models Attribute constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozef Marko committed Oct 1, 2019
1 parent a2a3fdc commit 309dac8
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.drools.workbench.models.datamodel.rule;

import java.util.Objects;
import java.util.stream.Stream;

import org.kie.soup.project.datamodel.oracle.DataType;

public enum Attribute {
SALIENCE("salience", DataType.TYPE_NUMERIC_INTEGER),
ENABLED("enabled", DataType.TYPE_BOOLEAN),
DATE_EFFECTIVE("date-effective", DataType.TYPE_DATE),
DATE_EXPIRES("date-expires", DataType.TYPE_DATE),
NO_LOOP("no-loop", DataType.TYPE_BOOLEAN),
AGENDA_GROUP("agenda-group", DataType.TYPE_STRING),
ACTIVATION_GROUP("activation-group", DataType.TYPE_STRING),
DURATION("duration", DataType.TYPE_NUMERIC_LONG),
TIMER("timer", DataType.TYPE_STRING),
CALENDARS("calendars", DataType.TYPE_STRING),
AUTO_FOCUS("auto-focus", DataType.TYPE_BOOLEAN),
LOCK_ON_ACTIVE("lock-on-active", DataType.TYPE_BOOLEAN),
RULEFLOW_GROUP("ruleflow-group", DataType.TYPE_STRING),
DIALECT("dialect", DataType.TYPE_STRING),
NEGATE_RULE("negate", DataType.TYPE_BOOLEAN);

private final String attributeName;

private final String dataType;

Attribute(final String attributeName, final String dataType) {
this.attributeName = attributeName;
this.dataType = dataType;
}

public String getAttributeName() {
return attributeName;
}

public static String getAttributeDataType(final String name) {
return Stream.of(Attribute.values())
.filter(a -> Objects.equals(a.getAttributeName(), name))
.findFirst()
.map(a -> a.dataType)
.orElse(DataType.TYPE_STRING);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

package org.drools.workbench.models.datamodel.rule;

import static org.drools.workbench.models.datamodel.rule.Attribute.AUTO_FOCUS;
import static org.drools.workbench.models.datamodel.rule.Attribute.CALENDARS;
import static org.drools.workbench.models.datamodel.rule.Attribute.DURATION;
import static org.drools.workbench.models.datamodel.rule.Attribute.ENABLED;
import static org.drools.workbench.models.datamodel.rule.Attribute.LOCK_ON_ACTIVE;
import static org.drools.workbench.models.datamodel.rule.Attribute.NO_LOOP;
import static org.drools.workbench.models.datamodel.rule.Attribute.SALIENCE;
import static org.drools.workbench.models.datamodel.rule.Attribute.TIMER;

/**
* This holds values for rule attributes (eg salience, agenda-group etc).
*/
public class RuleAttribute {

private static final String NOLOOP = "no-loop";
private static final String SALIENCE = "salience";
private static final String ENABLED = "enabled";
private static final String DURATION = "duration";
private static final String TIMER = "timer";
private static final String LOCK_ON_ACTIVE = "lock-on-active";
private static final String AUTO_FOCUS = "auto-focus";
private static final String CALENDARS = "calendars";

public RuleAttribute( final String name,
final String value ) {
this.attributeName = name;
Expand All @@ -45,26 +45,26 @@ public RuleAttribute() {
public String toString() {
StringBuilder ret = new StringBuilder();
ret.append( this.attributeName );
if ( NOLOOP.equals( attributeName ) ) {
if ( NO_LOOP.getAttributeName().equals( attributeName ) ) {
ret.append( " " );
ret.append( this.value == null ? "true" : this.value );
} else if ( SALIENCE.equals( this.attributeName ) ||
DURATION.equals( this.attributeName ) ) {
} else if ( SALIENCE.getAttributeName().equals( this.attributeName ) ||
DURATION.getAttributeName().equals( this.attributeName ) ) {
ret.append( " " );
ret.append( this.value );
} else if ( ENABLED.equals( this.attributeName ) ||
AUTO_FOCUS.equals( this.attributeName ) ||
LOCK_ON_ACTIVE.equals( this.attributeName ) ) {
} else if (ENABLED.getAttributeName().equals( this.attributeName ) ||
AUTO_FOCUS.getAttributeName().equals( this.attributeName ) ||
LOCK_ON_ACTIVE.getAttributeName().equals( this.attributeName ) ) {
ret.append( " " );
ret.append( this.value.equals( "true" ) ? "true" : "false" );
} else if ( TIMER.equals( this.attributeName ) ) {
} else if (TIMER.getAttributeName().equals( this.attributeName ) ) {
ret.append( " " );
if ( this.value.startsWith( "(" ) && this.value.endsWith( ")" ) ) {
ret.append( this.value );
} else {
ret.append( "(" ).append( this.value ).append( ")" );
}
} else if ( CALENDARS.equals( this.attributeName ) ) {
} else if (CALENDARS.getAttributeName().equals( this.attributeName ) ) {
final String raw = this.value.replaceAll( "\"|\\s", "" );
final String[] calendars = raw.split( "," );
ret.append( " " );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.drools.workbench.models.datamodel.rule.ActionCallMethod;
Expand Down Expand Up @@ -82,6 +83,8 @@
import org.kie.soup.project.datamodel.oracle.DataType;
import org.kie.soup.project.datamodel.oracle.OperatorsOracle;

import static org.drools.workbench.models.datamodel.rule.Attribute.NEGATE_RULE;

/**
* This takes care of converting GuidedDT object to DRL (via the RuleModel).
*/
Expand Down Expand Up @@ -884,7 +887,7 @@ void doAttribs(List<BaseColumn> allColumns,
if (validateAttributeCell(cell)) {

//If instance of "otherwise" column then flag RuleModel as being negated
if (at.getAttribute().equals(GuidedDecisionTable52.NEGATE_RULE_ATTR)) {
if (Objects.equals(at.getAttribute(), NEGATE_RULE.getAttributeName())) {
rm.setNegated(Boolean.valueOf(cell));
} else {
attribs.add(new RuleAttribute(at.getAttribute(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
*/
package org.drools.workbench.models.guided.dtable.backend.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.drools.workbench.models.guided.dtable.shared.model.ActionCol52;
import org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52;
import org.drools.workbench.models.guided.dtable.shared.model.ActionRetractFactCol52;
Expand All @@ -38,12 +44,6 @@
import org.kie.soup.project.datamodel.commons.IUpgradeHelper;
import org.kie.soup.project.datamodel.oracle.DataType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
* Helper class to upgrade model used for Guided Decision Table. This
* implementation converts legacy GuidedDecisionTable objects to
Expand All @@ -56,7 +56,6 @@ public class GuidedDecisionTableUpgradeHelper1

/**
* Convert the legacy Decision Table model to the new
*
* @param legacyDTModel
* @return The new DTModel
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
*/
package org.drools.workbench.models.guided.dtable.backend.util;

import java.util.List;
import java.util.Objects;

import org.drools.workbench.models.datamodel.rule.Attribute;
import org.drools.workbench.models.guided.dtable.shared.model.AttributeCol52;
import org.drools.workbench.models.guided.dtable.shared.model.BaseColumn;
import org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52;
import org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52;
import org.kie.soup.project.datamodel.commons.IUpgradeHelper;

import java.util.List;
import static org.drools.workbench.models.datamodel.rule.Attribute.DURATION;
import static org.drools.workbench.models.datamodel.rule.Attribute.SALIENCE;

/**
* Helper class to upgrade data-types for Guided Decision Table. This
Expand All @@ -34,7 +39,6 @@ public class GuidedDecisionTableUpgradeHelper2

/**
* Convert the data-types in the Decision Table model
*
* @param source
* @return The new DTModel
*/
Expand All @@ -53,10 +57,10 @@ public GuidedDecisionTable52 upgrade(GuidedDecisionTable52 source) {
final BaseColumn column = allColumns.get(iCol);
if (column instanceof AttributeCol52) {
AttributeCol52 attributeCol = (AttributeCol52) column;
final String attributeName = attributeCol.getAttribute();
if (GuidedDecisionTable52.SALIENCE_ATTR.equals(attributeName)) {
final String attributeName = attributeCol.getAttribute();
if (Objects.equals(SALIENCE.getAttributeName(), attributeName)) {
iSalienceColumnIndex = iCol;
} else if (GuidedDecisionTable52.DURATION_ATTR.equals(attributeName)) {
} else if (Objects.equals(DURATION.getAttributeName(), attributeName)) {
iDurationColumnIndex = iCol;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.drools.workbench.models.guided.dtable.shared.model.MetadataCol52;
import org.drools.workbench.models.guided.dtable.shared.validation.DecisionTableValidator;

import static org.drools.workbench.models.datamodel.rule.Attribute.ACTIVATION_GROUP;
import static org.drools.workbench.models.datamodel.rule.Attribute.SALIENCE;

/**
* Validates and extends the model based on selected HitPolicy
* @see org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52.HitPolicy
Expand Down Expand Up @@ -107,7 +110,7 @@ private RowPriorityResolver getRowPriorityResolver( final int priorityColumnInde

private int makeSalienceColumn() {
final AttributeCol52 attributeCol = new AttributeCol52();
attributeCol.setAttribute( "salience" );
attributeCol.setAttribute( SALIENCE.getAttributeName() );
dtable.getAttributeCols()
.add( attributeCol );

Expand Down Expand Up @@ -152,7 +155,7 @@ private int getRowNumberColumnIndex() {

private void addActivationGroup( final String activationGroupType ) {
final AttributeCol52 attributeCol52 = new AttributeCol52();
attributeCol52.setAttribute( GuidedDecisionTable52.ACTIVATION_GROUP_ATTR );
attributeCol52.setAttribute( ACTIVATION_GROUP.getAttributeName() );
dtable.getAttributeCols()
.add( attributeCol52 );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2011 Red Hat, Inc. and/or its affiliates.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Expand All @@ -17,6 +17,8 @@

import java.util.List;

import org.drools.workbench.models.datamodel.rule.Attribute;

/**
* This is a rule attribute - eg salience, no-loop etc.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2011 Red Hat, Inc. and/or its affiliates.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Expand All @@ -28,6 +28,7 @@
import org.drools.workbench.models.guided.dtable.shared.model.adaptors.FactPatternPattern52Adaptor;
import org.kie.soup.project.datamodel.imports.HasImports;
import org.kie.soup.project.datamodel.imports.Imports;
import org.kie.soup.project.datamodel.oracle.DataType;
import org.kie.soup.project.datamodel.packages.HasPackageName;

/**
Expand All @@ -47,25 +48,6 @@ public class GuidedDecisionTable52 implements HasImports,
*/
public static final int INTERNAL_ELEMENTS = 2;

/**
* Various attribute names
*/
public static final String SALIENCE_ATTR = "salience";
public static final String ENABLED_ATTR = "enabled";
public static final String DATE_EFFECTIVE_ATTR = "date-effective";
public static final String DATE_EXPIRES_ATTR = "date-expires";
public static final String NO_LOOP_ATTR = "no-loop";
public static final String AGENDA_GROUP_ATTR = "agenda-group";
public static final String ACTIVATION_GROUP_ATTR = "activation-group";
public static final String DURATION_ATTR = "duration";
public static final String TIMER_ATTR = "timer";
public static final String CALENDARS_ATTR = "calendars";
public static final String AUTO_FOCUS_ATTR = "auto-focus";
public static final String LOCK_ON_ACTIVE_ATTR = "lock-on-active";
public static final String RULEFLOW_GROUP_ATTR = "ruleflow-group";
public static final String DIALECT_ATTR = "dialect";
public static final String NEGATE_RULE_ATTR = "negate";

private String tableName;

private String parentName;
Expand Down Expand Up @@ -147,7 +129,6 @@ public List<AttributeCol52> getAttributeCols() {

/**
* Return an immutable list of Pattern columns
*
* @return
*/
public List<Pattern52> getPatterns() {
Expand Down Expand Up @@ -277,7 +258,6 @@ public List<List<DTCellValue52>> getData() {
* knowledge of individual columns is necessary; for example separate
* columns in the user-interface or where individual columns need to be
* analysed.
*
* @return A List of individual columns
*/
public List<BaseColumn> getExpandedColumns() {
Expand Down Expand Up @@ -399,7 +379,6 @@ public void setHitPolicy(final HitPolicy hitPolicy) {

/**
* Retrieve, or lazily instantiate a new, AuditLog.
*
* @return
*/
public AuditLog getAuditLog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.drools.workbench.models.guided.dtable.shared.util;

import org.drools.workbench.models.datamodel.rule.Attribute;
import org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint;
import org.drools.workbench.models.datamodel.rule.DSLSentence;
import org.drools.workbench.models.datamodel.rule.FactPattern;
Expand Down Expand Up @@ -88,34 +89,7 @@ private String getType(final RowNumberCol52 col) {
}

private String getType(final AttributeCol52 col) {
String type = DataType.TYPE_STRING;
final String attrName = col.getAttribute();
if (attrName.equals(GuidedDecisionTable52.SALIENCE_ATTR)) {
type = DataType.TYPE_NUMERIC_INTEGER;
} else if (attrName.equals(GuidedDecisionTable52.ENABLED_ATTR)) {
type = DataType.TYPE_BOOLEAN;
} else if (attrName.equals(GuidedDecisionTable52.NO_LOOP_ATTR)) {
type = DataType.TYPE_BOOLEAN;
} else if (attrName.equals(GuidedDecisionTable52.DURATION_ATTR)) {
type = DataType.TYPE_NUMERIC_LONG;
} else if (attrName.equals(GuidedDecisionTable52.TIMER_ATTR)) {
type = DataType.TYPE_STRING;
} else if (attrName.equals(GuidedDecisionTable52.CALENDARS_ATTR)) {
type = DataType.TYPE_STRING;
} else if (attrName.equals(GuidedDecisionTable52.AUTO_FOCUS_ATTR)) {
type = DataType.TYPE_BOOLEAN;
} else if (attrName.equals(GuidedDecisionTable52.LOCK_ON_ACTIVE_ATTR)) {
type = DataType.TYPE_BOOLEAN;
} else if (attrName.equals(GuidedDecisionTable52.DATE_EFFECTIVE_ATTR)) {
type = DataType.TYPE_DATE;
} else if (attrName.equals(GuidedDecisionTable52.DATE_EXPIRES_ATTR)) {
type = DataType.TYPE_DATE;
} else if (attrName.equals(GuidedDecisionTable52.DIALECT_ATTR)) {
type = DataType.TYPE_STRING;
} else if (attrName.equals(GuidedDecisionTable52.NEGATE_RULE_ATTR)) {
type = DataType.TYPE_BOOLEAN;
}
return type;
return Attribute.getAttributeDataType(col.getAttribute());
}

private String getType(final ConditionCol52 col) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Set;

import org.drools.workbench.models.datamodel.rule.Attribute;
import org.drools.workbench.models.guided.dtable.shared.model.AttributeCol52;
import org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52;

Expand Down
Loading

0 comments on commit 309dac8

Please sign in to comment.