⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.java

📁 Sematic Assessment System: Java GUI application that store all data in an Ontology in protege.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* Generated By:JavaCC: Do not edit this line. Parser.java */
package edu.stanford.smi.protege.storage.clips;

import java.util.*;
import java.util.logging.*;
import edu.stanford.smi.protege.model.*;
import edu.stanford.smi.protege.util.*;

public final class Parser implements ParserConstants {
    private KnowledgeBase itsKB;
    private Collection _itsErrors;
    private boolean isInclude;
    private Cls itsDefaultMetaCls;
    private Map itsInverseSlotMap = new HashMap();
    private Map itsSuperslotsMap = new HashMap();
    private Collection itsUndefinedClses = new HashSet();
    private Collection itsUndefinedInstances = new HashSet();
    private String state;
    private static final String LOADING_CLASSES = "loading classes";
    private static final String LOADING_INSTANCES = "loading instances";

    public String toString() {
        return "ClipsParser";
    }

    private void recordError(String msg) {
        recordError(msg, null);
    }

    private void recordError(String msg, Throwable e) {
        Object error = (e == null) ? (Object) msg : e;
        _itsErrors.add(error);
        Log.getLogger().log(Level.SEVERE, msg, e);
    }



    public void loadClses(KnowledgeBase kb, boolean isIncluded, Collection errors) {
        try {
            state = LOADING_CLASSES;
            itsKB = kb;
            itsDefaultMetaCls = kb.getCls(Model.Cls.STANDARD_CLASS);
            this.isInclude = isIncluded;
            _itsErrors = errors;
            parseOntology();
            connectInverseSlots();
            connectSuperslots();
            checkUndefinedClasses();
        } catch (Exception e) {
                recordError("Error parsing classes", e);
        }
    }

    private static final String UNDEFINED_CLASS = ":UNDEFINED";
    private Cls getUndefinedSuperclass() {
        Cls cls = itsKB.getCls(UNDEFINED_CLASS);
        if (cls == null) {
            Collection parents = CollectionUtilities.createCollection(itsKB.getRootCls());
            cls = itsKB.createCls(UNDEFINED_CLASS, parents);
        }
        return cls;
    }

    private void checkUndefinedClasses() {
        if (!itsUndefinedClses.isEmpty()) {
            Cls standardCls = itsKB.getDefaultClsMetaCls();
            Cls undefinedSuperclass = getUndefinedSuperclass();
            Iterator i = itsUndefinedClses.iterator();
            while (i.hasNext()) {
                Cls cls = (Cls) i.next();
                recordError("Undefined class: " + cls.getName() + " found while " + state);
                cls.addDirectSuperclass(undefinedSuperclass);
                cls.setDirectType(standardCls);
            }
            itsUndefinedClses.clear();
        }
    }

    private void checkUndefinedInstances() {
        if (!itsUndefinedInstances.isEmpty()) {
            Cls cls = getUndefinedSuperclass();
            Iterator i = itsUndefinedInstances.iterator();
            while (i.hasNext()) {
                Instance instance = (Instance) i.next();
                recordError("Undefined instance: " + instance.getName());
                instance.setDirectType(cls);
            }
        }
    }

    public void loadInstances(KnowledgeBase kb, boolean isIncluded, Collection errors) {
        try {
            state = LOADING_INSTANCES;
            itsKB = kb;
            this.isInclude = isIncluded;
            _itsErrors = errors;
                  parseKnowledgeBase();
            checkUndefinedClasses();
            checkUndefinedInstances();
        } catch (Exception e) {
                recordError("Error loading instances", e);
        }
    }

    private void handleException(Throwable e) {
        recordError("Parsing error", e);
        Token t;
        do {
            t = getNextToken();
        } while ((t.kind != EOF) && (t.kind != RPAREN && getToken(1).kind != LPAREN));
    }

    private void nameConflictError(String name, String typeName, Frame frame) {
        String text = name + " is both a " + typeName + " and a " + getTypeName(frame);
        recordError(text, null);
    }

    private String getTypeName(Frame frame) {
        String typeName;
        if (frame instanceof Cls) {
            typeName = "class";
        } else if (frame instanceof Slot) {
            typeName = "slot";
        } else if (frame instanceof Facet) {
            typeName = "facet";
        } else if (frame instanceof SimpleInstance) {
            typeName = "simple instance";
        } else {
            typeName = frame.getClass().getName();
        }
        return typeName;
    }

    private int nClses;
    private static int MIN_TRACE_COUNT = 5000;
    private static int TRACE_COUNT_DELTA = 1000;
    private static int MIN_CLASS_TRACE_COUNT = MIN_TRACE_COUNT;
    private static int MIN_INSTANCE_TRACE_COUNT = MIN_TRACE_COUNT;
    private static int CLASS_TRACE_COUNT_DELTA = TRACE_COUNT_DELTA;
    private static int INSTANCE_TRACE_COUNT_DELTA = TRACE_COUNT_DELTA;

    private Cls getCls(String name, String comment, Collection parents, boolean isDefinition) {
        if (name.equals(ClipsUtil.TOP_LEVEL_SLOT_CLASS)) {
            return null;
        }

        if (name.equals("USER")) {
            name = itsKB.getRootCls().getName();
        }

        Cls cls;
        Frame frame = itsKB.getFrame(name);
        if (frame == null) {
            cls = itsKB.createCls(name, parents, itsDefaultMetaCls, false);
            if ((++nClses % CLASS_TRACE_COUNT_DELTA) == 0 && nClses >= MIN_CLASS_TRACE_COUNT) {
                Log.getLogger().info("Loaded " + nClses + " classes: " + name);
            }
            if (comment != null) {
                cls.setDocumentation(comment);
            }
            if (!isDefinition) {
                itsUndefinedClses.add(cls);
            }
        } else if (frame instanceof Cls) {
            cls = (Cls) frame;
            if (isDefinition) {
                boolean succeeded = itsUndefinedClses.remove(cls);
                if (succeeded) {
                            if (comment != null) {
                                cls.setDocumentation(comment);
                            }
                            if (!parents.isEmpty()) {
                                addDirectSuperclasses(cls, parents);
                            }
                } else {
                    recordError("Duplicate definition of class " + name + " was ignored");
                }
            }
        } else if (frame instanceof SimpleInstance && ((Instance) frame).getDirectType() == null) {
            itsKB.setDirectType((Instance) frame, itsKB.getDefaultClsMetaCls());
            cls = itsKB.getCls(name);
            Iterator i = parents.iterator();
            while (i.hasNext()) {
                Cls parent = (Cls) i.next();
                cls.addDirectSuperclass(parent);
            }
        } else {
            nameConflictError(name, "class", frame);
            cls = getCls(name + "_", comment, parents, isDefinition);
        }
        if (isInclude) {
            cls.setIncluded(true);
        }
        return cls;
    }

    private void addDirectSuperclasses(Cls cls, Collection parents) {
        Iterator i = parents.iterator();
        while (i.hasNext()) {
            Cls parent = (Cls) i.next();
            cls.addDirectSuperclass(parent);
        }
    }

    private boolean hasSlot(String name) {
        return itsKB.containsFrame(name);
    }

    private Slot getExistingSlot(String name) {
        Slot slot;
        Frame frame = itsKB.getFrame(name);
        if (frame instanceof Slot) {
            slot = (Slot) frame;
        } else {
            recordError("non-existent slot: " + name);
            slot = getSlot(name);
        }
        return slot;
    }

    private Slot getSlot(String name) {
        Slot slot;
        Frame frame = itsKB.getFrame(name);
        if (frame == null) {
            slot = itsKB.createSlot(name, itsKB.getDefaultSlotMetaCls(), false);
        } else if (frame instanceof Slot) {
            slot = (Slot) frame;
        } else {
            nameConflictError(name, "slot", frame);
            slot = getSlot(name + "_");
        }
        if (isInclude) {
            slot.setIncluded(true);
        }
        return slot;
    }

    private int nInstances;
    private int nSetTypeInstances;
    private Instance getInstance(String instanceName, Collection types) {
        Instance instance = itsKB.getInstance(instanceName);
        if (instance == null) {
                Collection directTypes = types == null ? Collections.EMPTY_LIST : types;
            instance = itsKB.createInstance(new FrameID(instanceName), directTypes, false);
            if ((++nInstances % INSTANCE_TRACE_COUNT_DELTA) == 0 && nInstances >= MIN_INSTANCE_TRACE_COUNT) {
                Log.getLogger().info("Loaded " + nInstances + " instances: " + instanceName);
            }
            if (types == null) {
                itsUndefinedInstances.add(instance);
            }
            if (instance instanceof Cls) {
                itsUndefinedClses.add(instance);
            }
        } else if (types != null && !CollectionUtilities.equalsSet(instance.getDirectTypes(), types)) {
            if (isTypeMismatch(instance, types)) {
                recordError("type mismatch: " + instance + " cannot become an instance of " + types);
            }
            Instance oldInstance = instance;
            instance = instance.setDirectTypes(types);
            if ((++nSetTypeInstances % INSTANCE_TRACE_COUNT_DELTA) == 0 && nSetTypeInstances >= MIN_INSTANCE_TRACE_COUNT) {
                Log.getLogger().info("Set type on " + nSetTypeInstances + " instances: " + instanceName);
            }
            itsUndefinedInstances.remove(oldInstance);
        }
        if (isInclude) {
            instance.setIncluded(true);
        }
        return instance;
    }

    private static boolean isTypeMismatch(Instance instance, Collection types) {
        boolean isTypeMismatch;
        Cls cls = (Cls) CollectionUtilities.getFirstItem(types);
        if (instance instanceof Cls) {
            isTypeMismatch = !cls.isClsMetaCls();
        } else if (instance instanceof Slot) {
            isTypeMismatch = !cls.isSlotMetaCls();
        } else if (instance instanceof Facet) {
            isTypeMismatch = !cls.isFacetMetaCls();
        } else {
            isTypeMismatch = false;
        }
        return isTypeMismatch;
    }

    private void checkBooleanAndSetValues(Cls cls, Slot slot, Collection values) {
        if (areBooleanValues(values)) {
            if (cls == null) {
                Collection defaults = slot.getDefaultValues();
                defaults = convertToBooleanDefaults(defaults);
                slot.setValueType(ValueType.BOOLEAN);
                slot.setDefaultValues(defaults);
            } else {
                Collection defaults = cls.getTemplateSlotDefaultValues(slot);
                defaults = convertToBooleanDefaults(defaults);
                cls.setTemplateSlotValueType(slot, ValueType.BOOLEAN);
                cls.setTemplateSlotDefaultValues(slot, defaults);
            }
        } else {
            if (cls == null) {
                slot.setAllowedValues(values);
            } else {
                cls.setTemplateSlotAllowedValues(slot, values);
            }
        }
    }
        /*
    private void checkBoolean(Cls cls, Slot slot) {
        Collection values = (cls == null) ? slot.getAllowedValues() : cls.getTemplateSlotAllowedValues(slot);
        if (areBooleanValues(values)) {
            if (cls == null) {
                slot.setValueType(ValueType.BOOLEAN);
                Collection defaults = slot.getDefaultValues();
                defaults = convertToBooleanDefaults(defaults);
                slot.setDefaultValues(defaults);
            } else {
                cls.setTemplateSlotValueType(slot, ValueType.BOOLEAN);
                Collection defaults = cls.getTemplateSlotDefaultValues(slot);
                defaults = convertToBooleanDefaults(defaults);
                cls.setTemplateSlotDefaultValues(slot, defaults);
            }
        }
    }
    */

    private boolean areBooleanValues(Collection values) {
        boolean areBooleanValues = false;
        if (values.size() == 2) {
            Iterator i = values.iterator();
            String v1 = (String) i.next();
            if (isBooleanValue(v1)) {
                String v2 = (String) i.next();
                if (isBooleanValue(v2)) {
                    areBooleanValues = !v1.equalsIgnoreCase(v2);
                }
            }
        }
        return areBooleanValues;
    }

    private Collection convertToBooleanDefaults(Collection c) {
        Collection booleans;
        if (c.isEmpty()) {
            booleans = Collections.EMPTY_LIST;
        } else {
            booleans = new ArrayList();
            Iterator i = c.iterator();
            while (i.hasNext()) {
                Object o = i.next();
                booleans.add(Boolean.valueOf(o.toString()));
            }
        }
        return booleans;
    }

    private static boolean isBooleanValue(String s) {
        return s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false");
    }

    private void checkedValueAdd(Collection values, ValueType type, Object value) {
        try {
            tryCheckedValueAdd(values, type, value);
        } catch (Exception e) {
            recordError("invalid object type", e);
        }
    }

    public static boolean equals(Object o1, Object o2) {
        return SystemUtilities.equals(o1, o2);
    }

    private void tryCheckedValueAdd(Collection values, ValueType type, Object value) {
        if (type == ValueType.CLS && value instanceof String) {
            value = getCls((String)value, null, Collections.EMPTY_LIST, false);
        } else if (type == ValueType.INSTANCE && value instanceof String) {
            value = getInstance((String)value, null);
        } else if (type == ValueType.FLOAT) {
            value = toFloat((String)value);
        } else if (type == ValueType.INTEGER) {
            value = toInteger((String)value);
        } else if (type == ValueType.BOOLEAN) {
            value = new Boolean((String)value);
        } else if (type == ValueType.ANY && value instanceof String) {
            // here we are essentially guessing
            Object first = CollectionUtilities.getFirstItem(values);
            if (first != null && appearsToBeValueType(values)) {
                value = getInstance((String) value, null);
            }
        }
        if (value == null) {
            throw new IllegalArgumentException("null value");
        }
        values.add(value);
    }

    private static boolean appearsToBeValueType(Collection values) {
        boolean isValueType = false;
        Object first = CollectionUtilities.getFirstItem(values);
        if (first != null) {
            isValueType = first.equals(ValueType.INSTANCE.toString()) || first.equals(ValueType.CLS.toString());
        }
        return isValueType;
    }

    Slot createTemplateSlot(String name, boolean isSingle, Cls cls, boolean isTopLevel) {
        Slot slot = getSlot(name);
        if (cls != null && !cls.hasTemplateSlot(slot)) {
            cls.addDirectTemplateSlot(slot);
        }
        if (!isSingle) {
            if (isTopLevel) {
                slot.setAllowsMultipleValues(true);
            } else {
                cls.setTemplateSlotAllowsMultipleValues(slot, true);
            }
        }
        return slot;
    }

    ValueType getSlotType(Cls cls, Slot slot) {
        return (cls == null) ? slot.getValueType() : cls.getTemplateSlotValueType(slot);
    }

    private static final String VARIABLE = "?VARIABLE";

    private Integer toInteger(String s) {
        Integer i = null;
        try {
            if (!s.equals(VARIABLE)) {
                i = new Integer(s);
            }
        } catch (Exception e) {
            try {
                Float f = new Float(s);
                i = new Integer(f.intValue());
            } catch (Exception e2) {
                recordError("Invalid integer: " + s, e2);
                i = null;
            }
        }
        return i;
    }

    private Float toFloat(String s) {
        Float f = null;
        try {
            if (!s.equals(VARIABLE)) {
                f = new Float(s);
            }
        } catch (Exception e) {
            recordError("Invalid floating point number: " + s, e);
            f = null;
        }
        return f;
    }

    private void addInverseSlotPair(Slot slot, Slot inverseSlot) {
        Slot existingSlot = (Slot) itsInverseSlotMap.get(inverseSlot);
        if (existingSlot == null) {
            itsInverseSlotMap.put(slot, inverseSlot);
        } else if (!equals(existingSlot, slot)) {
            recordError("mismatched inverse slots: " + slot + ", " + inverseSlot + ", " + existingSlot);
        } else {
            // do nothing
        }
    }

    private void addSuperslots(Slot slot, Collection superslots) {
        itsSuperslotsMap.put(slot, superslots);
    }

    private void connectInverseSlots() {
        Iterator i = itsInverseSlotMap.keySet().iterator();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -