org.drools.core.reteoo.compiled.DeclarationsHandler Maven / Gradle / Ivy
/*
* Copyright 2010 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.core.reteoo.compiled;
import org.drools.core.base.ClassFieldReader;
import org.drools.core.reteoo.AlphaNode;
import org.drools.core.reteoo.BetaNode;
import org.drools.core.reteoo.LeftInputAdapterNode;
import org.drools.core.reteoo.Sink;
import org.drools.core.rule.ContextEntry;
import java.util.*;
/**
* This handler is used to create the member declarations section of a generated subclass of a {@link CompiledNetwork}.
* Currently we only create member variables for the following types of nodes:
*
* Non-hashed {@link AlphaNode}s
* {@link LeftInputAdapterNode}s
* {@link BetaNode}s
* A {@link Map} for each set of hashed {@link AlphaNode}s. The keys are the hashed values, and the values are
* the IDs of the alphas
*/
public class DeclarationsHandler extends AbstractCompilerHandler {
private static final String PRIVATE_MODIFIER = "private";
/**
* This field keeps track of the current set of hashed AlphaNodes for a ClassReader.
*
* @see #startHashedAlphaNodes(org.kie.base.ClassFieldReader)
* @see #startHashedAlphaNode(org.kie.reteoo.AlphaNode, Object)
*/
private HashedAlphasDeclaration currentHashedAlpha;
private final StringBuilder builder;
/**
* Keeps track of all the ClassFieldReaders for hashed alphas, and the maps that contain hashed values/node ids
* for said alphas.
*/
private final Collection hashedAlphaDeclarations;
public DeclarationsHandler(StringBuilder builder) {
this.builder = builder;
this.hashedAlphaDeclarations = new LinkedList();
}
private String getVariableDeclaration(AlphaNode alphaNode) {
Class variableType = getVariableType(alphaNode);
String variableName = getVariableName(alphaNode);
// comment for variable declaration is just the toString of the node
String comment = alphaNode.toString();
return PRIVATE_MODIFIER + " " + variableType.getName() + " " + variableName + "; // " + comment;
}
private String getContextVariableDeclaration(AlphaNode alphaNode){
Class variableType = ContextEntry.class;
String variableName = getContextVariableName(alphaNode);
return PRIVATE_MODIFIER + " " + variableType.getName() + " " + variableName + ";";
}
private String getVariableDeclaration(Sink sink) {
Class declarationType = getVariableType(sink);
String variableName = getVariableName(sink);
// comment for variable declaration is just the toString of the node
String comment = sink.toString();
return PRIVATE_MODIFIER + " " + declarationType.getName() + " " + variableName + "; // " + comment;
}
private String getVariableDeclaration(ClassFieldReader fieldReader) {
Class declarationType = Map.class;
Class createType = HashMap.class;
String variableName = getVariableName(fieldReader);
// todo JANINO doesn't support generics
// return "private java.util.Map