Skip to content
Closed
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
Expand Up @@ -1027,13 +1027,13 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {

// is it a regular fact or an event?
AnnotationDescr annotationDescr = typeDescr.getAnnotation( TypeDeclaration.Role.ID );
String role = (annotationDescr != null) ? annotationDescr.getValue() : null;
String role = (annotationDescr != null) ? annotationDescr.getSingleValue() : null;
if ( role != null ) {
type.setRole( TypeDeclaration.Role.parseRole( role ) );
}

annotationDescr = typeDescr.getAnnotation( TypeDeclaration.ATTR_TYPESAFE );
String typesafe = (annotationDescr != null) ? annotationDescr.getValue() : null;
String typesafe = (annotationDescr != null) ? annotationDescr.getSingleValue() : null;
if ( typesafe != null ) {
type.setTypesafe( Boolean.parseBoolean( typesafe ) );
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {

// is it a POJO or a template?
annotationDescr = typeDescr.getAnnotation( TypeDeclaration.ATTR_TEMPLATE );
String templateName = (annotationDescr != null) ? annotationDescr.getValue() : null;
String templateName = (annotationDescr != null) ? annotationDescr.getSingleValue() : null;
if ( templateName != null ) {
type.setFormat( TypeDeclaration.Format.TEMPLATE );
FactTemplate template = pkgRegistry.getPackage().getFactTemplate( templateName );
Expand All @@ -1077,7 +1077,7 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {
}
} else {
annotationDescr = typeDescr.getAnnotation( TypeDeclaration.ATTR_CLASS );
String className = (annotationDescr != null) ? annotationDescr.getValue() : null;
String className = (annotationDescr != null) ? annotationDescr.getSingleValue() : null;

if ( StringUtils.isEmpty( className ) ) {
className = type.getTypeName();
Expand Down Expand Up @@ -1112,7 +1112,7 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {
}

annotationDescr = typeDescr.getAnnotation( TypeDeclaration.ATTR_TIMESTAMP );
String timestamp = (annotationDescr != null) ? annotationDescr.getValue() : null;
String timestamp = (annotationDescr != null) ? annotationDescr.getSingleValue() : null;
if ( timestamp != null ) {
type.setTimestampAttribute( timestamp );
ClassDefinition cd = type.getTypeClassDef();
Expand All @@ -1123,7 +1123,7 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {
}

annotationDescr = typeDescr.getAnnotation( TypeDeclaration.ATTR_DURATION );
String duration = (annotationDescr != null) ? annotationDescr.getValue() : null;
String duration = (annotationDescr != null) ? annotationDescr.getSingleValue() : null;
if ( duration != null ) {
type.setDurationAttribute( duration );
ClassDefinition cd = type.getTypeClassDef();
Expand All @@ -1134,7 +1134,7 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {
}

annotationDescr = typeDescr.getAnnotation( TypeDeclaration.ATTR_EXPIRE );
String expiration = (annotationDescr != null) ? annotationDescr.getValue() : null;
String expiration = (annotationDescr != null) ? annotationDescr.getSingleValue() : null;
if ( expiration != null ) {
if ( timeParser == null ) {
timeParser = new TimeIntervalParser();
Expand Down
25 changes: 16 additions & 9 deletions drools-compiler/src/main/java/org/drools/lang/DRLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,13 @@ public ImportDescr importStatement() throws RecognitionException {
DroolsEditorType.KEYWORD );
if ( state.failed ) return null;

if ( helper.validateIdentifierKey( DroolsSoftKeywords.FUNCTION ) ) {
String kwd;
if ( helper.validateIdentifierKey( kwd = DroolsSoftKeywords.FUNCTION ) ||
helper.validateIdentifierKey( kwd = DroolsSoftKeywords.STATIC ) ){
// function
match( input,
DRLLexer.ID,
DroolsSoftKeywords.FUNCTION,
kwd,
null,
DroolsEditorType.KEYWORD );
if ( state.failed ) return null;
Expand Down Expand Up @@ -425,8 +427,8 @@ public TypeDeclarationDescr declare() throws RecognitionException {
DroolsEditorType.KEYWORD );
if ( state.failed ) return null;

// type
String type = type();
// type may be qualified when adding metadata
String type = qualifiedIdentifier();
if ( state.failed ) return null;
if ( state.backtracking == 0 ) declare.type( type );

Expand All @@ -436,7 +438,9 @@ public TypeDeclarationDescr declare() throws RecognitionException {
if ( state.failed ) return null;
}

while ( input.LA( 1 ) == DRLLexer.ID && !helper.validateIdentifierKey( DroolsSoftKeywords.END ) ) {
boolean qualified = type.indexOf( '.' ) >= 0;
while ( ! qualified &&
input.LA( 1 ) == DRLLexer.ID && !helper.validateIdentifierKey( DroolsSoftKeywords.END ) ) {
// field*
field( declare );
if ( state.failed ) return null;
Expand Down Expand Up @@ -493,7 +497,7 @@ private void field( DeclareDescrBuilder declare ) {
if ( state.failed ) return;

// type
String type = type();
String type = qualifiedIdentifier();
if ( state.failed ) return;
if ( state.backtracking == 0 ) field.type( type );

Expand Down Expand Up @@ -1790,7 +1794,7 @@ private BaseDescr lhsUnary( final CEDescrBuilder< ? , ? > ce,
result = lhsEval( ce );
} else if ( helper.validateIdentifierKey( DroolsSoftKeywords.FORALL ) ) {
result = lhsForall( ce );
} else if ( helper.validateIdentifierKey( DroolsSoftKeywords.ACCUMULATE ) ) {
// } else if ( helper.validateIdentifierKey( DroolsSoftKeywords.ACCUMULATE ) ) {
// TODO: handle this
} else if ( input.LA( 1 ) == DRLLexer.LEFT_PAREN ) {
// the order here is very important: this if branch must come before the lhsPatternBind bellow
Expand Down Expand Up @@ -2257,7 +2261,8 @@ private void failMissingTokenException() throws MissingTokenException {
*/
private void lhsPattern( PatternDescrBuilder< ? > pattern,
String label ) throws RecognitionException {
String type = type();
// String type = unadornedType();
String type = this.qualifiedIdentifier();
if ( state.failed ) return;

if ( state.backtracking == 0 ) {
Expand Down Expand Up @@ -3240,12 +3245,14 @@ private String elementValueArrayInitializer() {
/* ------------------------------------------------------------------------------------------------
* UTILITY RULES
* ------------------------------------------------------------------------------------------------ */

/**
* Matches a type name
*
* type := ID typeArguments? ( DOT ID typeArguments? )* (LEFT_SQUARE RIGHT_SQUARE)*
*
* @param doQualify set to true if qualification is acceptable
* @param doGenPar set to true if generic arguments and brackets are acceptable
* @return
* @throws RecognitionException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,16 @@ private String formatParserLocation() {
StringBuilder sb = new StringBuilder();
if ( paraphrases != null ) {
for ( Map<DroolsParaphraseTypes, String> map : paraphrases ) {
for ( Entry<DroolsParaphraseTypes, String> activeEntry : map
.entrySet() ) {
for ( Entry<DroolsParaphraseTypes, String> activeEntry : map.entrySet() ) {
if ( activeEntry.getValue().length() == 0 ) {
sb.append( String.format( PARSER_LOCATION_MESSAGE_PART,
getLocationName( activeEntry.getKey() ) ) );
String kStr = getLocationName( activeEntry.getKey() );
if( kStr.length() > 0 ){
sb.append( String.format( PARSER_LOCATION_MESSAGE_PART, kStr ) );
}
} else {
sb.append( String.format( PARSER_LOCATION_MESSAGE_COMPLETE,
getLocationName( activeEntry.getKey() ),
activeEntry
.getValue() ) );
activeEntry.getValue() ) );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,8 @@ void setEnd( DescrBuilder< ? > db ) {
setStart( builderContext.empty() ? null : builderContext.peek() );
} else if ( ImportDescrBuilder.class.isAssignableFrom( clazz ) ) {
ImportDescrBuilder imp;
if ( validateLT( 2,
DroolsSoftKeywords.FUNCTION ) ) {
if ( validateLT( 2, DroolsSoftKeywords.FUNCTION ) ||
validateLT( 2, DroolsSoftKeywords.STATIC ) ) {
imp = (builderContext.empty()) ?
DescrFactory.newPackage().newFunctionImport() :
((PackageDescrBuilder) builderContext.peek()).newFunctionImport();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.drools.lang.descr;

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

/*
* Copyright 2011 JBoss Inc
Expand All @@ -19,12 +22,22 @@
* limitations under the License.
*/
public class AnnotationDescr extends BaseDescr {

private static final String VALUE = "value";
private static final long serialVersionUID = 520l;

private final String name;
private Map<String, String> values;

private static String unquote( String s ){
if( s.startsWith( "\"" ) && s.endsWith( "\"" ) ||
s.startsWith( "'" ) && s.endsWith( "'" ) ) {
return s.substring( 1, s.length() - 1 );
} else {
return s;
}
}

public AnnotationDescr(final String name) {
this.name = name;
this.values = new HashMap<String, String>();
Expand All @@ -34,8 +47,7 @@ public AnnotationDescr(final String name,
final String value) {
this.name = name;
this.values = new HashMap<String, String>();
this.values.put( VALUE,
value );
this.values.put( VALUE, value );
}

public String getName() {
Expand All @@ -46,29 +58,66 @@ public boolean hasValue() {
return !this.values.isEmpty();
}

public String getValue() {
return this.values.get( VALUE );
}

public void setValue( final String value ) {
this.values.put( VALUE,
value );
this.values.put( VALUE, value );
}

public void setKeyValue( final String key,
final String value ) {
this.values.put( key,
value );
this.values.put( key, value );
}

public String getValue( final String key ) {
return this.values.get( key );
}

public Map<String, String> getValues() {
return values;
public Map<String,String> getValues(){
return this.values;
}

/**
* Returns the metadata value as a single object or a Map
* @return
*/
public Object getValue() {
Object single = getSingleValue();
return single != null ? single : this.values;
}

public Object getValueStripped() {
Object single = getSingleValueStripped();
if( single != null ) return single;
Map<String,String> sMap = new HashMap<String,String>();
for( Map.Entry<String,String> entry: this.values.entrySet() ){
sMap.put( entry.getKey(), unquote( entry.getValue() ) );
}
return sMap;
}

public Map<String,String> getValueMap() {
return this.values;
}

public String getSingleValue(){
Set<String> keySet = this.values.keySet();
if( keySet.size() == 1 &&
"value".equals( keySet.iterator().next() ) ){
return this.values.get( "value" );
} else {
return null;
}
}

public String getSingleValueStripped(){
Set<String> keySet = this.values.keySet();
if( keySet.size() == 1 &&
"value".equals( keySet.iterator().next() ) ){
return unquote( this.values.get( "value" ) );
} else {
return null;
}
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,8 @@ public void build(final RuleBuildContext context) {
public void buildMetaAttributes(final RuleBuildContext context ) {
Rule rule = context.getRule();
for ( String metaAttr : context.getRuleDescr().getAnnotationNames() ) {
String value = context.getRuleDescr().getAnnotation(metaAttr).getValue();
if( value.startsWith( "\"" ) && value.endsWith( "\"" ) && value.length() > 2 ) {
value = value.substring( 1, value.length()-1 );
}
rule.addMetaAttribute( metaAttr, value );
rule.addMetaAttribute( metaAttr,
context.getRuleDescr().getAnnotation(metaAttr).getValueStripped() );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void visit(List<TypeDeclarationDescr> typeDeclarationDescrs) {
}

for (String metadata : typeDeclaration.getAnnotations().keySet()) {
Map<String, String> values = typeDeclaration.getAnnotation(metadata).getValues();
Map<String, String> values = typeDeclaration.getAnnotation(metadata).getValueMap();
for (String value : values.keySet()) {
objectType.getMetadata().put(metadata,
value);
Expand Down