Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DROOLS-4425: Refactor drools-workbench-models Attribute constants #2508

Merged
merged 6 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2019 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
* License for the specific language governing permissions and limitations under
* the License.
*/

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

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

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Can we add some comment why do we need this attribute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dupliaka thank you, I added a javadoc in a new commit

/**
* This enum collects all attributes supported by kie tooling - guided rule editors, guided decision table editor ...
* The enum stores pairs (attribute name, attribute data type)
* The 'attribute data type' determines what HTML element will be shown for user to set the attribute value
* e.g. TYPE_BOOLEAN -> checkbox, TYPE_DATE -> date picker ...
*/
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 @@ -43,40 +43,36 @@ public RuleAttribute() {
}

public String toString() {
StringBuilder ret = new StringBuilder();
final StringBuilder ret = new StringBuilder();
ret.append( this.attributeName );
if ( NOLOOP.equals( attributeName ) ) {
ret.append( " " );
ret.append( ' ' );
if ( NO_LOOP.getAttributeName().equals( attributeName ) ) {
ret.append( this.value == null ? "true" : this.value );
} else if ( SALIENCE.equals( this.attributeName ) ||
DURATION.equals( this.attributeName ) ) {
ret.append( " " );
} else if ( SALIENCE.getAttributeName().equals( this.attributeName ) ||
DURATION.getAttributeName().equals( this.attributeName ) ) {
ret.append( this.value );
} else if ( ENABLED.equals( this.attributeName ) ||
AUTO_FOCUS.equals( this.attributeName ) ||
LOCK_ON_ACTIVE.equals( this.attributeName ) ) {
ret.append( " " );
} else if (ENABLED.getAttributeName().equals( this.attributeName ) ||
AUTO_FOCUS.getAttributeName().equals( this.attributeName ) ||
LOCK_ON_ACTIVE.getAttributeName().equals( this.attributeName ) ) {
ret.append( this.value.equals( "true" ) ? "true" : "false" );
} else if ( TIMER.equals( this.attributeName ) ) {
ret.append( " " );
} else if (TIMER.getAttributeName().equals( this.attributeName ) ) {
if ( this.value.startsWith( "(" ) && this.value.endsWith( ")" ) ) {
ret.append( this.value );
} else {
ret.append( "(" ).append( this.value ).append( ")" );
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( " " );
for ( String calendar : calendars ) {
ret.append( "\"" ).append( calendar ).append( "\", " );
ret.append( '"' ).append( calendar ).append( "\", " );
}
ret.delete( ret.length() - 2,
ret.length() );
} else if ( this.value != null ) {
ret.append( " \"" );
ret.append( '"' );
ret.append( this.value );
ret.append( "\"" );
ret.append( '"' );
}
return ret.toString();
}
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,17 @@
*/
package org.drools.workbench.models.guided.dtable.backend.util;

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

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 +38,6 @@ public class GuidedDecisionTableUpgradeHelper2

/**
* Convert the data-types in the Decision Table model
*
* @param source
* @return The new DTModel
*/
Expand All @@ -53,10 +56,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 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 Down Expand Up @@ -47,25 +47,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 +128,6 @@ public List<AttributeCol52> getAttributeCols() {

/**
* Return an immutable list of Pattern columns
*
* @return
*/
public List<Pattern52> getPatterns() {
Expand Down Expand Up @@ -277,7 +257,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 +378,6 @@ public void setHitPolicy(final HitPolicy hitPolicy) {

/**
* Retrieve, or lazily instantiate a new, AuditLog.
*
* @return
*/
public AuditLog getAuditLog() {
Expand Down
Loading