001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.brms.server.util;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
025: import org.drools.brms.client.modeldriven.brl.DSLSentence;
026:
027: /**
028: * A builder to incrementally populate a SuggestionCompletionEngine
029: *
030: * @author etirelli
031: */
032: public class SuggestionCompletionEngineBuilder {
033:
034: private SuggestionCompletionEngine instance = new SuggestionCompletionEngine();
035: private List factTypes = new ArrayList();
036: private Map fieldsForType = new HashMap();
037: private Map fieldTypes = new HashMap();
038: private Map globalTypes = new HashMap();
039: private List actionDSLSentences = new ArrayList();
040: private List conditionDSLSentences = new ArrayList();
041:
042: public SuggestionCompletionEngineBuilder() {
043: }
044:
045: /**
046: * Start the creation of a new SuggestionCompletionEngine
047: */
048: public void newCompletionEngine() {
049: this .instance = new SuggestionCompletionEngine();
050: this .factTypes = new ArrayList();
051: this .fieldsForType = new HashMap();
052: this .fieldTypes = new HashMap();
053: this .globalTypes = new HashMap();
054: this .actionDSLSentences = new ArrayList();
055: this .conditionDSLSentences = new ArrayList();
056: }
057:
058: /**
059: * Adds a fact type to the engine
060: *
061: * @param factType
062: */
063: public void addFactType(final String factType) {
064: this .factTypes.add(factType);
065: }
066:
067: /**
068: * Adds the list of fields for a given type
069: *
070: * @param type
071: * @param fields
072: */
073: public void addFieldsForType(final String type,
074: final String[] fields) {
075: this .fieldsForType.put(type, fields);
076: }
077:
078: /**
079: * @return true if this has the type already registered (field information).
080: */
081: public boolean hasFieldsForType(final String type) {
082: return this .fieldsForType.containsKey(type);
083: }
084:
085: /**
086: * Adds a type declaration for a field
087: *
088: * @param field
089: * @param type
090: */
091: public void addFieldType(final String field, final String type) {
092: this .fieldTypes.put(field, type);
093: }
094:
095: /**
096: * Adds a global and its corresponding type to the engine
097: *
098: * @param global
099: * @param type
100: */
101: public void addGlobalType(final String global, final String type) {
102: this .globalTypes.put(global, type);
103: }
104:
105: /**
106: * Add a DSL sentence for an action.
107: */
108: public void addDSLActionSentence(final String sentence) {
109: final DSLSentence sen = new DSLSentence();
110: sen.sentence = sentence;
111: this .actionDSLSentences.add(sen);
112: }
113:
114: /**
115: * Add a DSL sentence for a condition.
116: */
117: public void addDSLConditionSentence(final String sentence) {
118: final DSLSentence sen = new DSLSentence();
119: sen.sentence = sentence;
120: this .conditionDSLSentences.add(sen);
121: }
122:
123: /**
124: * Returns a SuggestionCompletionEngine instance populated with
125: * all the data since last call to newCompletionEngine() method
126: *
127: * @return
128: */
129: public SuggestionCompletionEngine getInstance() {
130: this .instance.factTypes = (String[]) this .factTypes
131: .toArray(new String[this .factTypes.size()]);
132: this .instance.fieldsForType = this .fieldsForType;
133: this .instance.fieldTypes = this .fieldTypes;
134: this .instance.globalTypes = this .globalTypes;
135: this .instance.actionDSLSentences = (DSLSentence[]) this .actionDSLSentences
136: .toArray(new DSLSentence[this .actionDSLSentences.size()]);
137: this .instance.conditionDSLSentences = (DSLSentence[]) this .conditionDSLSentences
138: .toArray(new DSLSentence[this.conditionDSLSentences
139: .size()]);
140: return this.instance;
141: }
142:
143: }
|