All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.core.rule.DynamicFact Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
/*
 * 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.rule;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.kie.api.definition.type.FactField;
import org.kie.api.definition.type.FactType;

/**
 * DO NOT USE THIS YET. Use FactType instead.
 */
public class DynamicFact implements Map {

    private Object bean;
    private FactType typeDef;

    public DynamicFact(Object bean, FactType typeDef) {
        this.bean = bean;
        this.typeDef = typeDef;
    }

    public void clear() {
        throw new UnsupportedOperationException();
    }

    public boolean containsKey(Object key) {
        return this.typeDef.getField((String) key) != null;
    }

    public boolean containsValue(Object arg0) {
        throw new UnsupportedOperationException();
    }

    public Set entrySet() {

        Set set = new HashSet();

        List list = this.typeDef.getFields();
        for (FactField factField : list) {
            final FactField ff = factField;
            Map.Entry ent = new Map.Entry() {

                public String getKey() {
                    return ff.getName();
                }

                public Object getValue() {
                    return typeDef.get(bean, ff.getName());
                }

                public Object setValue(Object o) {
                    typeDef.set(bean, ff.getName(), o);
                    return o;
                }

            };
        }

        return set;

    }

    public Object get(Object fieldName) {
        return this.typeDef.get(bean, (String) fieldName);
    }

    public boolean isEmpty() {
        return this.typeDef.getFields().size() == 0;
    }

    public Set keySet() {
        throw new UnsupportedOperationException();
    }

    public Object put(String key, Object value) {
        Object old = this.typeDef.get(bean, key);
        this.typeDef.set(this.bean, key, value);
        return old;
    }

    public void putAll(Map arg0) {
        throw new UnsupportedOperationException();
    }

    public Object remove(Object arg0) {
        throw new UnsupportedOperationException();
    }

    public int size() {
        return this.typeDef.getFields().size();
    }

    public Collection values() {
        throw new UnsupportedOperationException();
    }

    /**
     * Return the underlying fact itself. This is what you asset into the session/engine.
     */
    public Object getFactObject() {
        return this.bean;
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy