Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.cyc.kb.client;
/*
* #%L
* File: KbObjectImpl.java
* Project: KB Client
* %%
* Copyright (C) 2013 - 2017 Cycorp, Inc
* %%
* 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.
* #L%
*/
import com.cyc.base.CycAccess;
import com.cyc.base.CycAccessManager;
import com.cyc.base.cycobject.CycAssertion;
import com.cyc.base.cycobject.CycConstant;
import com.cyc.base.cycobject.CycList;
import com.cyc.base.cycobject.CycObject;
import com.cyc.base.cycobject.CycSymbol;
import com.cyc.base.cycobject.CycVariable;
import com.cyc.base.cycobject.DenotationalTerm;
import com.cyc.base.cycobject.Fort;
import com.cyc.base.cycobject.Guid;
import com.cyc.base.cycobject.Naut;
import com.cyc.base.exception.CycApiException;
import com.cyc.base.exception.CycConnectionException;
import com.cyc.baseclient.CycServerInfoImpl;
import com.cyc.baseclient.cycobject.CycArrayList;
import com.cyc.baseclient.cycobject.CycConstantImpl;
import com.cyc.baseclient.cycobject.DefaultCycObjectImpl;
import com.cyc.baseclient.cycobject.FormulaSentenceImpl;
import com.cyc.baseclient.cycobject.NautImpl;
import com.cyc.baseclient.datatype.DateConverter;
import static com.cyc.baseclient.subl.functions.CycEvaluateFunction.CYC_EVALUATE_INDEXICAL;
import com.cyc.baseclient.subl.functions.CycEvaluateFunction.UnevaluatableExpressionException;
import static com.cyc.baseclient.subl.functions.SublFunctions.INDEXICAL_P;
import com.cyc.kb.Context;
import com.cyc.kb.KbCollection;
import com.cyc.kb.KbFactory;
import com.cyc.kb.KbFunction;
import com.cyc.kb.KbIndividual;
import com.cyc.kb.KbObject;
import com.cyc.kb.KbPredicate;
import com.cyc.kb.KbTerm;
import com.cyc.kb.Sentence;
import com.cyc.kb.Symbol;
import com.cyc.kb.exception.CreateException;
import com.cyc.kb.exception.KbException;
import com.cyc.kb.exception.KbRuntimeException;
import com.cyc.kb.exception.KbServerSideException;
import com.cyc.kb.exception.KbTypeException;
import com.cyc.kb.exception.VariableArityException;
import com.cyc.nl.Paraphraser;
import com.cyc.nl.ParaphraserFactory;
import com.cyc.session.CycSession;
import com.cyc.session.CycSessionManager;
import com.cyc.session.exception.SessionCommunicationException;
import com.cyc.session.exception.SessionConfigurationException;
import com.cyc.session.exception.SessionException;
import com.cyc.session.exception.SessionInitializationException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The base class for all the other classes in this package. Each KBObject is
* basically a facade for an object in the Cyc KB, and as such it provides
* common methods to make, retrieve, and remove CycL Assertions.
*
*
* @param type of CycObject core
*
* @author Vijay Raj
* @version "$Id: KbObjectImpl.java 173082 2017-07-28 15:36:55Z nwinant $"
*/
public class KbObjectImpl implements KbObject {
//====| Static fields |===================================================================//
private static final Logger LOG = LoggerFactory.getLogger(KbObjectImpl.class.getCanonicalName());
private static final CycConstant THE_LIST = new CycConstantImpl(
"TheList", new Guid("bdcc9f7c-9c29-11b1-9dad-c379636f7270"));
private static final CycConstant THE_EMPTY_LIST = new CycConstantImpl(
"TheEmptyList", new Guid("bd79c885-9c29-11b1-9dad-c379636f7270"));
private static final CycConstant THE_SET = new CycConstantImpl(
"TheSet", new Guid("bd58e476-9c29-11b1-9dad-c379636f7270"));
private static final CycConstant THE_EMPTY_SET = new CycConstantImpl(
"TheEmptySet", new Guid("bdf8edae-9c29-11b1-9dad-c379636f7270"));
//====| Static factory methods |==========================================================//
/**
* Attempts to return a CycObject (or a Java primitive object) based on the
* KBObject (or a Java primitive object).
*
* A CycObject or a subclass of it, is the primary representation of the BaseClient.
* It represents the same concept in the KB as the KBObject. The CycObject is
* useful when the user has to do something the KB API does not support.
*
* @param arg the inputs KBObject that will be converted to a CycObject
*
* @return the CycObject representation of the input ARG
*/
public static Object convertKBObjectToCycObject(Object arg) {
if (arg instanceof KbObject) {
return ((KbObject) arg).getCore();
} else if (arg instanceof List) {
if (((List) arg).isEmpty()) {
return THE_EMPTY_LIST;
} else {
CycList cl = new CycArrayList();
for (Object listElem : (List)arg) {
cl.add(convertKBObjectToCycObject(listElem));
}
Naut cn = new NautImpl(THE_LIST, cl.toArray());
return cn;
}
} else if (arg instanceof Set) {
if (((Set)arg).isEmpty()) {
return THE_EMPTY_SET;
} else {
CycList cl = new CycArrayList();
for (Object setElem : (Set)arg) {
cl.add(convertKBObjectToCycObject(setElem));
}
Naut cn = new NautImpl(THE_SET, cl.toArray());
return cn;
}
} else if (arg instanceof Date) {
DateConverter.getInstance();
CycObject co = DateConverter.toCycDate((Date) arg);
return co;
} else {
return arg;
}
}
/**
* Attempts to return an Object (expected to be of type T) for the input
* Object o.
*
* For basic Java objects like String, Number and Date, the object is returned
* without any modification. CycObjects are converted to KBObjects, of the
* most specific type possible.
*
* @param
* @param o object to be mapped to KBObject
*
* @return the KBObject constructed.
* @throws CreateException
*/
public static T checkAndCastObject(Object o) throws CreateException {
if (o instanceof CycObject) {
return (T) KbObjectImpl.convertCycObject((CycObject) o);
} else if (o instanceof String || o instanceof Number || o instanceof Date) {
return (T) o;
} else {
throw new IllegalArgumentException("Unable to coerce " + o + "(" + o.getClass() + ").");
// return null;
}
}
@Deprecated
public static KbObjectImpl from(KbObject obj) {
return (KbObjectImpl) obj;
}
private static Object convertCycObject(CycObject cyco) throws CreateException {
try {
// First try converting to a Set, List, or Date:
if (cyco instanceof CycArrayList) {
CycList cl = (CycArrayList) cyco;
if (cl.get(0) instanceof CycConstantImpl) {
try {
final KbTerm kbt = KbObjectImplFactory.findOrCreate((CycConstant) cl.get(0), KbTermImpl.class);
if (kbt instanceof KbFunction) {
KbFunction kbf = (KbFunction) kbt;
// Do not check arity if its a VariableArityFunction, since that check throws an exception
if ((kbf.isVariableArity() || kbf.getArity() == cl.size() - 1)
&& kbf.isUnreifiable()) {
cyco = new NautImpl(cl);
}
} else if (kbt instanceof KbPredicate) {
KbPredicate kbp = (KbPredicate) kbt;
// Do not check arity if its a VariableArityFunction, since that check throws an exception
if (kbp.isVariableArity() || kbp.getArity() == cl.size() - 1) {
cyco = new FormulaSentenceImpl(cl);
} else if (kbt instanceof LogicalConnectiveImpl || kbt instanceof QuantifierImpl) {
cyco = new FormulaSentenceImpl(cl);
}
}
} catch (KbTypeException | CreateException | VariableArityException e) {
// ignore and move on
}
}
}
// handle an empty list
if (cyco instanceof CycConstant) {
final CycConstant c = (CycConstant) cyco;
if (c.equals(THE_EMPTY_LIST)) {
return new ArrayList<>();
} else if (c.equals(THE_EMPTY_SET)) {
return new HashSet<>();
}
}
if (cyco instanceof Naut) {
final Naut cn = (Naut) cyco;
final DenotationalTerm functor = (cn).getFunctor();
if (functor.equals(THE_SET) || functor.equals(THE_LIST)) {
final Collection