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

edu.dlsu.SUMOs.main.PictureEditor Maven / Gradle / Ivy

Go to download

Sigma knowledge engineering system is an system for developing, viewing and debugging theories in first order logic. It works with Knowledge Interchange Format (KIF) and is optimized for the Suggested Upper Merged Ontology (SUMO) www.ontologyportal.org.

There is a newer version: 2.10
Show newest version
package edu.dlsu.SUMOs.main;

import java.awt.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.StringTokenizer;

import com.articulate.sigma.KBmanager;
import com.articulate.sigma.Vampire;

import edu.dlsu.SUMOs.obj.Input;
import edu.dlsu.SUMOs.obj.Instance;
import edu.dlsu.SUMOs.obj.Rule;
import edu.dlsu.SUMOs.util.DateUtils;
import edu.dlsu.SUMOs.util.ReadWriteTextFile;

public class PictureEditor {

    private HashMap hashmap = new HashMap();
    private static ArrayList multipleAttributes;
    private static int MAXDAYS = 1;
    private static int days = 0;
    
    //for statistics only
    private static String timeBegin = "";
    private static String timeEnd = "";
    private static boolean generalCount = false;
    private static boolean conflictCount = false;
    private static int general_count = 0;
    private static int conflict_count = 0;
    private static int query_count = 0;
    private static int assert_count = 0;
    
    // vampire query parameters
    private final static int TIME_lIMIT = 30;
    private final static int RESULTS_LIMIT = 10;
    
    // print in console
    //        isPrint = print queries and results
    //        isPrint2 = print assertions
    public static boolean isPrint = true;
    public static boolean isPrint2 = true;
    
    // themes
    private final static int THEME_BEHONEST = 1;
    //private final static int THEME_EATGOODFOOD = 2;
    private final static int THEME_SLEEPEARLY = 3;
    private final static int THEME_BEBRAVE = 4;
    private final static int THEME_BECAREFUL = 5;
    
    // characters
    private final static int FEMALE_CHILD_RABBIT = 1;
    private final static int MALE_CHILD_RABBIT = 2;
    private final static int FEMALE_ADULT_RABBIT = 3;
    private final static int MALE_ADULT_RABBIT = 4;
    private final static int FEMALE_ADULT_ELEPHANT = 5;
    private final static int FEMALE_CHILD_ELEPHANT = 6;
    
    //objects
    private final static int OBJECT_LAMP = 1;
    private final static int OBJECT_CANDY = 2;
    private final static int OBJECT_TOYS = 3;
    private final static int OBJECT_DRINKINGCUP = 4;
    
    //backgrounds
    private final static int BG_LIVINGROOM = 1; 
    private final static int BG_CLASSROOM = 2;
    
    private boolean isChangePhase = false;
    private boolean reset = false;
    private String log = "";
    private boolean writeLog = true;
    private boolean saveResults = true;
    private boolean isConflict = false;

    private boolean getCaseRoles = false;
    private boolean allowDuplicates = true;
    private ArrayList phaseKnowledge;
    private String phase;
    private ArrayList omissibleKnowledge;
    private final String DATABASE = System.getenv("SIGMA_HOME")+"\\inference\\SUMO-v.kif";
    private String result;
    private ArrayList resettableAttributes;
    private ArrayList resettableKnowledge;
    private ArrayList results;
    private ArrayList oldKnowledge;
    private ArrayList oneTimeEventsAndAttributes;
    private int instanceCnt;
    private Vampire vampire;
    private ArrayList instances;
    private Collection operationalKnowledge;
    private Collection childCharacters;
    private Rule rule;
    private Instance childCharacter;
    private Instance adultCharacter;
    private Instance object;
    private Instance background;
    private Instance story;
    private static boolean doneSomething = false;
    
    public PictureEditor(final Input input) {
        try {    
            System.out.println("**********Picture Editor v0.4**********");
            initializeSettings();
            initializeStory(input);
            initializeTime(input);
            assertCharacters(input);
            
            assertObjects(input);
            assertBackground(input.getBackground());
            obtainLocations();
                 
            // introduction phase: introduce the names of the child characters
            for(int i=0;i();
        oneTimeEventsAndAttributes.add("ChangeDayEvent");
        multipleAttributes = new ArrayList();
        multipleAttributes.add("TraitAttribute");
        multipleAttributes.add("PositiveAttribute");
        multipleAttributes.add("NegativeAttribute");
    }

    private void followRule() {
        System.out.println("*****follow rule*****");
        String instance = rule.getInstance();
        
        // if rule is instance
        if(instance!=null) {
            submitFormula("instance", instance, instance);
            if(rule.getAgent()!=null) {
                submitFormula("agent",instances.get(instances.size()-1).toString(),rule.getAgent());
            }
            if(rule.getExperiencer()!=null) {
                submitFormula("experiencer",instances.get(instances.size()-1).toString(),rule.getExperiencer());
            }
            if(rule.getManner()!=null) {
                submitFormula("manner",instances.get(instances.size()-1).toString(),rule.getManner());
            }
            if(rule.getPatient()!=null) {
                submitFormula("patient",instances.get(instances.size()-1).toString(),rule.getPatient());
            }
        } else {
            // if rule is attribute
            submitFormula(rule.getRule());
        }
    }

    private void tell(String sender, String receiver, String rule) {
        submitFormula("instance", "Stating", "Stating");
        String orderingInstance = (instances.get(instances.size()-1)).toString();
        submitFormula("experiencer", orderingInstance, sender);
        submitFormula("patient", orderingInstance, receiver);
        submitFormula("patient", orderingInstance, rule);
    }

    private void initializeTime(Input input) {
        switch(input.getTheme()) {
            case THEME_SLEEPEARLY:
                submitFormula("instance", "NightTime", "NightTime");
                break;
            default:
                submitFormula("instance", "DayTime", "DayTime");
        }
        submitFormula("instance","Friday","Friday");
    }

    private void checkNewEvents() {
        System.out.println("*****checkNewEvents()");
        // check any new instances
        submitQuery("instance", "?X", "StoryEvent");
                
        if(!(results.get(0).equals("no"))) {
            ArrayList eventList = results;
            for(int j=0;j();
        operationalKnowledge = new LinkedHashSet();
        oldKnowledge = new ArrayList();
                
        submitFormula("instance", "Story", "Story");
        story = new Instance("Story" + instanceCnt);
        setTheme(input);
        this.phase = "Introduction";
        changeStoryPhase("Introduction");
    }

    private void setTheme(Input input) {
        switch(input.getTheme()) {
            case THEME_SLEEPEARLY:
                submitFormula("attribute",story.getName(),"SleepEarly");
                break;
            case THEME_BEBRAVE:
                submitFormula("attribute",story.getName(),"BeBrave");
                break;
            case THEME_BEHONEST:
                submitFormula("attribute",story.getName(),"BeHonest");
                break;
            case THEME_BECAREFUL:
                submitFormula("attribute",story.getName(),"BeCareful");
                break;
        }
    }

    private void determineCapabilities(Instance instance) {
        System.out.println("*****determineCapabilities("+instance.getName()+")");
        doneSomething = false;
        // check capability character
        submitQuery("(or (capability ?X experiencer "+instance.getName()+") (capability ?X agent "+instance.getName()+"))");
        if(!results.get(0).equals("no")) {
            
            // check if he can only do it once in a phase
            String action = results.get(results.size() - 1).toString();
            boolean isNew = true;
            if (oneTimeEventsAndAttributes.contains(action) && phaseKnowledge.contains(action)) {
                isNew = false;
            }
            
            if(isNew) {
                // choose one of the possible actions
                submitFormula("instance", results.get(results.size() - 1).toString(), results.get(results.size() - 1).toString());
                
                // obtain the case roles from the capability chosen
                if(getCaseRoles) {
                    //System.out.println("determining case roles for "+results.get(results.size() - 1).toString());
                    determineCaseRolesFromCapability(results.get(results.size() - 1).toString(), instances.get(instances.size()-1).toString());
                    getCaseRoles = false;
                }
                else {
                    //System.out.println("skipping case roles for "+results.get(results.size() - 1).toString());
                }
            }
        }
        checkConflicts("");
    }

    /**
     * Generates the story plot
     */
    private void createLog() {
        try {
            File story = new File("story.txt");
            story.createNewFile();
            ReadWriteTextFile logWriter = new ReadWriteTextFile();
            logWriter.setContents(story, log);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * Check the attributes of all instances and creates a snapshot if any conflicts are found
     * Removes obsolete knowledge also if certain conditions are met (ex. a day passed)
     */
    private void checkConflicts(String phase) {
        do {
            doGeneralQueries();
            isConflict = false;
            System.out.println("*****checkConflicts("+phase+")");
            conflictCount = true;
            writeLog = false;
            System.out.println("********************");
            if(object!=null) {
                checkAttribute(object);
            }
            System.out.println("********************");
            for(int i=0;i**********");
            System.out.println("********************");
            if(reset) {
                deleteInstances();
                resetCharacters();
                reset = false;
            }
            removeKnowledge();
            if(isChangePhase) {
                while(operationalKnowledge.contains("(attribute Story1 "+phase+"Phase)")) {
                    operationalKnowledge.remove("(attribute Story1 "+phase+"Phase)");
                }
            }
            if(isConflict || isChangePhase) {
                System.out.println("Creating new snapshot.............................................................");
                try {
                    vampire.terminate();
                    vampire = Vampire.getNewInstance(DATABASE);
                    resubmitFormulas();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            writeLog = true;
            conflictCount = false;
            isChangePhase = false;
        } while (isConflict);
    }
    
    /**
     * Obtains the different case roles of the capability chosen and submits it to the operational knowledge
     * @param item
     */
    private void determineCaseRolesFromCapability(String action, String instance) {
        String caseRole = "";
        for(int i=0; i<6;i++) {
            switch(i) {
                case 0:
                    caseRole = "experiencer";
                    submitQuery("capability", action, "experiencer", "?X");
                    break;
                case 1:
                    caseRole = "patient";
                    submitQuery("capability", action, "patient", "?X");
                    break;
                case 2:
                    caseRole = "agent";
                    submitQuery("capability", action, "agent", "?X");
                    break;
                case 3:
                    caseRole = "origin";
                    submitQuery("capability", action, "origin", "?X");
                    break;
                case 4:
                    caseRole = "instrument";
                    submitQuery("capability", action, "instrument", "?X");
                    break;
                case 5:
                    caseRole = "manner";
                    submitQuery("capability", action, "manner", "?X");
                    break;
            }
            if(!(results.get(0).equals("no")||results.get(0).equals("yes"))) {
                for(int j=0;j attributes = results;
        if(i>0 && !results.get(0).equals("no")) {
            for(int j=0;j1) {
                                isConflict = true;
                                boolean found = false;
                                String remove = "";
                                System.out.print("CONFLICT DETECTED: ");
                                for(int k=0;k




© 2015 - 2024 Weber Informatics LLC | Privacy Policy