Skip to content
SmartSpaceKPI.java 9.12 KiB
Newer Older
SaboteurInCave's avatar
SaboteurInCave committed
package wrapper;

import sofia_kp.SIBResponse;
import sofia_kp.iKPIC_subscribeHandler2;
import sofia_kp.KPICore;

import java.util.ArrayList;
import java.util.Vector;

public class SmartSpaceKPI {

    private KPICore core;
    private ArrayList<String> subscriptionIdList;
Petrov Mikhail's avatar
Petrov Mikhail committed
    private ArrayList<SmartSpaceTriple> tripletList;
SaboteurInCave's avatar
SaboteurInCave committed

    public SmartSpaceKPI(String host, int port, String spaceName) throws SmartSpaceException {
        core = new KPICore(host, port, spaceName);
        subscriptionIdList = new ArrayList<String>();
Petrov Mikhail's avatar
Petrov Mikhail committed
        tripletList = new ArrayList<SmartSpaceTriple>();
SaboteurInCave's avatar
SaboteurInCave committed

        SIBResponse joinResponse = core.join();

        if (!joinResponse.isConfirmed())
            throw new SmartSpaceException(joinResponse.Message);

        core.disable_debug_message();
    }

Petrov Mikhail's avatar
Petrov Mikhail committed
    public void insert(SmartSpaceTriple triplet) throws SmartSpaceException {
SaboteurInCave's avatar
SaboteurInCave committed
        SIBResponse insertResponse = core.insert(triplet.getSubject(), triplet.getPredicate(), triplet.getObject(), triplet.getSubjectType(), triplet.getObjectType());
        if (!insertResponse.isConfirmed()) {
            String text = String.format("KPI failed to insert triplet: (%s, %s, %s, %s, %s)",
Petrov Mikhail's avatar
Petrov Mikhail committed
                    triplet.getSubject(), triplet.getPredicate(), triplet.getObject(),
                    triplet.getSubjectType(), triplet.getObjectType());
SaboteurInCave's avatar
SaboteurInCave committed

            throw new SmartSpaceException(text + '\n' + insertResponse.Message);
        }
    }

Petrov Mikhail's avatar
Petrov Mikhail committed
    public void remove(SmartSpaceTriple triplet) throws SmartSpaceException {
SaboteurInCave's avatar
SaboteurInCave committed
        SIBResponse removeResponse = core.remove(triplet.getSubject(), triplet.getPredicate(), triplet.getObject(), triplet.getSubjectType(), triplet.getObjectType());
        if (!removeResponse.isConfirmed()) {
            String text = String.format("KP failed to remove triplet: (%s, %s, %s, %s, %s)",
Petrov Mikhail's avatar
Petrov Mikhail committed
                    triplet.getSubject(), triplet.getPredicate(), triplet.getObject(),
                    triplet.getSubjectType(), triplet.getObjectType());
SaboteurInCave's avatar
SaboteurInCave committed

            throw new SmartSpaceException(text + '\n' + removeResponse.Message);
        }
    }

Petrov Mikhail's avatar
Petrov Mikhail committed
    public Vector<SmartSpaceTriple> query(SmartSpaceTriple triplet) throws SmartSpaceException {
SaboteurInCave's avatar
SaboteurInCave committed
        SIBResponse queryResponse = core.queryRDF(triplet.getSubject(), triplet.getPredicate(), triplet.getObject(), triplet.getSubjectType(), triplet.getObjectType());

        if (queryResponse.isConfirmed()) {
            Vector<Vector<String>> stringVectorResult = queryResponse.query_results;

Petrov Mikhail's avatar
Petrov Mikhail committed
            Vector<SmartSpaceTriple> result = new Vector<SmartSpaceTriple>();
SaboteurInCave's avatar
SaboteurInCave committed

            for (Vector<String> it : stringVectorResult) {
Petrov Mikhail's avatar
Petrov Mikhail committed
                result.add(new SmartSpaceTriple(it));
SaboteurInCave's avatar
SaboteurInCave committed
            }

            return result;
        } else {
            throw new SmartSpaceException(queryResponse.Message);
        }
    }

Petrov Mikhail's avatar
Petrov Mikhail committed
    public void update(SmartSpaceTriple newTriplet, SmartSpaceTriple oldTriplet) throws SmartSpaceException {
SaboteurInCave's avatar
SaboteurInCave committed
        SIBResponse updateResponse = core.update(
                newTriplet.getSubject(), newTriplet.getPredicate(), newTriplet.getObject(), newTriplet.getSubjectType(), newTriplet.getObjectType(),
                oldTriplet.getSubject(), oldTriplet.getPredicate(), oldTriplet.getObject(), oldTriplet.getSubjectType(), oldTriplet.getObjectType()
        );

        if (!updateResponse.isConfirmed()) {
            String text = String.format("KP failed to update triplet! Old triplet: (%s, %s, %s, %s, %s), new triplet (%s, %s, %s, %s, %s)",
                    newTriplet.getSubject(), newTriplet.getPredicate(), newTriplet.getObject(), newTriplet.getSubjectType(), newTriplet.getObjectType(),
                    oldTriplet.getSubject(), oldTriplet.getPredicate(), oldTriplet.getObject(), oldTriplet.getSubjectType(), oldTriplet.getObjectType());

            throw new SmartSpaceException(text + '\n' + updateResponse.Message);
        }
    }

Petrov Mikhail's avatar
Petrov Mikhail committed
    public String subscribe(SmartSpaceTriple triplet, iKPIC_subscribeHandler2 handler) throws SmartSpaceException {
SaboteurInCave's avatar
SaboteurInCave committed
        SIBResponse subscribeResponse = core.subscribeRDF(triplet.getSubject(), triplet.getPredicate(), triplet.getObject(), triplet.getObjectType(), handler);

        if (subscribeResponse != null && subscribeResponse.isConfirmed()) {
            subscriptionIdList.add(subscribeResponse.subscription_id);
Petrov Mikhail's avatar
Petrov Mikhail committed
            tripletList.add(triplet);
            return subscribeResponse.subscription_id;
SaboteurInCave's avatar
SaboteurInCave committed
        } else {
            System.err.println("Some problems with subscribing");
            throw new SmartSpaceException(subscribeResponse != null ? subscribeResponse.Message : null);
        }
    }

    public void leave() throws SmartSpaceException {

        try {
            unsubscribe();
Petrov Mikhail's avatar
Petrov Mikhail committed
        } catch (SmartSpaceException exception) {
SaboteurInCave's avatar
SaboteurInCave committed
            System.err.println(exception.getMessage());
        }

        SIBResponse leaveResponse = core.leave();

        if (!leaveResponse.isConfirmed()) {
            throw new SmartSpaceException(leaveResponse.Message);
        }
    }


    private void unsubscribe() throws SmartSpaceException {
        String exceptionMessage = "";

        for (String id : subscriptionIdList) {
            SIBResponse unsubscribeResponse = core.unsubscribe(id);

            // у нас проблемы с отпиской от интеллектуального пространства
            if (!unsubscribeResponse.isConfirmed()) {
                exceptionMessage += id + ": " + unsubscribeResponse.Message + '\n';
            }
        }

        subscriptionIdList.clear();

        // проблемы во время отписки были, сигнализируем это
        if (!exceptionMessage.isEmpty()) {
            throw new SmartSpaceException(exceptionMessage);
        }
    }
Petrov Mikhail's avatar
Petrov Mikhail committed


    public void unsubscribe(Iterable<String> subscriptionIds) throws SmartSpaceException {
        String exceptionMessage = "";

        for (String subscriptionId : subscriptionIds) {
            try {
                unsubscribe(subscriptionId);
            } catch (SmartSpaceException e) {
                exceptionMessage += e.getMessage() + "\n";
            }
        }

        // проблемы во время отписки были, сигнализируем это
        if (!exceptionMessage.isEmpty()) {
            throw new SmartSpaceException(exceptionMessage);
        }
    }

    public void unsubscribe(String subscriptionId) throws SmartSpaceException {
        SIBResponse unsubscribeResponse = core.unsubscribe(subscriptionId);

        // у нас проблемы с отпиской от интеллектуального пространства
        if (!unsubscribeResponse.isConfirmed()) {
            throw new SmartSpaceException(subscriptionId + ": " + unsubscribeResponse.Message + '\n');
        } else {
            int index = subscriptionIdList.indexOf(subscriptionId);
            if (index >= 0) {
                subscriptionIdList.remove(index);
                tripletList.remove(index);
            }
        }
    }

    public void unsubscribe(Iterable<SmartSpaceTriple> triplets, boolean fullMatch) throws SmartSpaceException {
        String exceptionMessage = "";

        for (SmartSpaceTriplet triplet : triplets)
            try {
                unsubscribe(triplet, fullMatch);
            } catch (SmartSpaceException e) {
                exceptionMessage += e.getMessage() + "\n";
            }

        // проблемы во время отписки были, сигнализируем это
        if (!exceptionMessage.isEmpty()) {
            throw new SmartSpaceException(exceptionMessage);
        }
    }

    public void unsubscribe(SmartSpaceTriplet triplet, boolean fullMatch) throws SmartSpaceException {
        String exceptionMessage = "";
Petrov Mikhail's avatar
Petrov Mikhail committed

        String subject = triplet.getSubject(), predicate = triplet.getPredicate(), object = triplet.getObject();

        ArrayList<String> oldIdList = new ArrayList<String>(subscriptionIdList);
        for (int i = oldIdList.size() - 1; i >= 0; i--) {
Petrov Mikhail's avatar
Petrov Mikhail committed
            SmartSpaceTriple curTriplet = tripletList.get(i);
Petrov Mikhail's avatar
Petrov Mikhail committed

Petrov Mikhail's avatar
Petrov Mikhail committed
            if (checkTwoStrings(subject, curTriplet.getSubject(), fullMatch) &&
                    checkTwoStrings(predicate, curTriplet.getPredicate(), fullMatch) &&
                    checkTwoStrings(object, curTriplet.getObject(), fullMatch)) {
                SIBResponse unsubscribeResponse = core.unsubscribe(oldIdList.get(i));
Petrov Mikhail's avatar
Petrov Mikhail committed

                // у нас проблемы с отпиской от интеллектуального пространства
                if (!unsubscribeResponse.isConfirmed()) {
                    exceptionMessage += oldIdList.get(i) + ": " + unsubscribeResponse.Message + '\n';
                } else {
                    subscriptionIdList.remove(i);
                    tripletList.remove(i);
Petrov Mikhail's avatar
Petrov Mikhail committed
                }
            }
Petrov Mikhail's avatar
Petrov Mikhail committed
        }

        // проблемы во время отписки были, сигнализируем это
        if (!exceptionMessage.isEmpty()) {
            throw new SmartSpaceException(exceptionMessage);
        }
    }

Petrov Mikhail's avatar
Petrov Mikhail committed
    private boolean checkTwoStrings(String mainString, String curString, boolean fullMatch) {
Petrov Mikhail's avatar
Petrov Mikhail committed
        if (mainString == null)
Petrov Mikhail's avatar
Petrov Mikhail committed
            return !fullMatch || (curString == null);
Petrov Mikhail's avatar
Petrov Mikhail committed
        return mainString.equals(curString);
    }
SaboteurInCave's avatar
SaboteurInCave committed
}