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: SentenceImpl.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.cycobject.CycConstant;
import com.cyc.base.cycobject.CycList;
import com.cyc.base.cycobject.CycObject;
import com.cyc.base.cycobject.CycVariable;
import com.cyc.base.cycobject.FormulaSentence;
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.CommonConstants;
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 com.cyc.kb.ArgPosition;
import com.cyc.kb.ArgUpdate;
import com.cyc.kb.Assertion;
import com.cyc.kb.Context;
import com.cyc.kb.KbObject;
import com.cyc.kb.KbTerm;
import com.cyc.kb.Quantifier;
import com.cyc.kb.Relation;
import com.cyc.kb.Sentence;
import com.cyc.kb.Variable;
import static com.cyc.kb.client.KbObjectImpl.getStaticAccess;
import com.cyc.kb.client.quant.QuantifiedRestrictedVariable;
import com.cyc.kb.client.quant.RestrictedVariable;
import com.cyc.kb.exception.CreateException;
import com.cyc.kb.exception.KbException;
import com.cyc.kb.exception.KbRuntimeException;
import com.cyc.kb.exception.KbTypeException;
import com.cyc.session.exception.SessionCommunicationException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A Sentence object is a Java representation of a syntactically well-formed
* #$CycLSentence.
*
* {@link com.cyc.kb.KbTerm Denotational terms} (including terms formed by
* {@link com.cyc.kb.KBFunction functions}) are not sentences, but can be included as elements of
* sentences. A syntactically well-formed Sentence consists of a {@link Relation} followed by any
* number of additional terms, which could be denotational terms or additional sentences. A sentence
* need not obey the arity restrictions of the top-level Relation in order to be syntactically
* well-formed. In other words, the only restriction that must be obeyed to construct a sentence is
* that there must be a relation in the 0th position.
*
* Unlike most of the objects in the KB API which correspond directly to an object on the Cyc
* server, sentences are merely combinations of objects that exist on the server, but the
* combination itself need not correspond to a server-side object. They can, however, still be used
* and understood by the server, and are used extensively to perform queries or make assertions.
*
* @author Vijay Raj
* @version $Id: SentenceImpl.java 173082 2017-07-28 15:36:55Z nwinant $
* @since 1.0
*/
public class SentenceImpl extends PossiblyNonAtomicKbObjectImpl implements Sentence {
//====| Static methods |==================================================================//
/**
* Conjoin sentences. Creates a list and calls {@link #and(java.lang.Iterable)}
*
* @param sentences list of sentences to be conjoined
*
* @return a new conjoined sentence
* @throws KbTypeException
* @throws com.cyc.kb.exception.CreateException
*/
public static Sentence and(Sentence... sentences) throws KbTypeException, CreateException {
return and(Arrays.asList(sentences));
}
/**
* Conjoin sentences. Creates a new sentence with #$and as the relation and all other sentences as
* the arguments.
*
* @param sentences list of sentences to be conjoined
*
* @return a new conjunction sentence
* @throws com.cyc.kb.exception.KbTypeException
* @throws com.cyc.kb.exception.CreateException
*/
public static Sentence and(Iterable sentences) throws KbTypeException, CreateException {
List cfsList = new ArrayList<>();
for (Sentence s : sentences) {
cfsList.add(toCycSentence(s));
}
final FormulaSentence cfs = FormulaSentenceImpl.makeConjunction(cfsList);
// TODO: Can we catch KBTypeException. We know all components are Sentences.
// combination should be a Sentence
return new SentenceImpl(cfs);
}
/**
* Disjoin sentences. Creates a list and calls {@link #or(java.lang.Iterable)}
*
* @param sentences list of sentences to be disjoined
*
* @return a new disjunction sentence
* @throws KbTypeException
* @throws com.cyc.kb.exception.CreateException
*/
public static Sentence or(Sentence... sentences) throws KbTypeException, CreateException {
return or(Arrays.asList(sentences));
}
/**
* Disjoin sentences. Creates a new sentence with #$or as the relation and all other sentences as
* the arguments.
*
* @param sentences list of sentences to be disjoined
*
* @return a new disjunction sentence
* @throws com.cyc.kb.exception.KbTypeException
* @throws com.cyc.kb.exception.CreateException
*/
public static Sentence or(Iterable sentences) throws KbTypeException, CreateException {
List cfsList = new ArrayList<>();
for (Sentence s : sentences) {
cfsList.add(toCycSentence(s));
}
final FormulaSentence cfs = FormulaSentenceImpl.makeDisjunction(cfsList);
// TODO: Can we catch KBTypeException. We know all components are Sentences.
// combination should be a Sentence
return new SentenceImpl(cfs);
}
public static Sentence implies(Collection posLiterals, Sentence negLiteral) throws KbTypeException, CreateException {
return implies(and(posLiterals), negLiteral);
}
public static Sentence implies(Sentence posLiteral, Sentence negLiteral) throws KbTypeException, CreateException {
final FormulaSentence conditional = FormulaSentenceImpl.makeConditional((FormulaSentence) posLiteral.getCore(), (FormulaSentence) negLiteral.getCore());
return new SentenceImpl(conditional);
}
/**
* Return the KBCollection as a KBObject of the Cyc term that underlies this class
* (CycLSentence).
*
* @return KBCollectionImpl.get("#$CycLSentence");
*/
public static KbObject getClassType() {
try {
return KbCollectionImpl.get(getClassTypeString());
} catch (KbException kae) {
throw new KbRuntimeException(kae.getMessage(), kae);
}
}
static String getClassTypeString() {
return "#$CycLSentence";
}
private static FormulaSentenceImpl toCycSentence(Sentence sentence) {
return (FormulaSentenceImpl) sentence.getCore();
}
/**
* Build a FormulaSentence from the given KBObjects arguments args. Note that
* args should either be KBObjects or Java classes, String, Number or Date. This
* method also handles java.util.List and java.util.Set of other supported KB API or Java objects.
* It even supports, List of List etc.
*
* @param args
*
* @return a FormulaSentence corresponding to the arguments args.
*/
public static FormulaSentence convertKBObjectArrayToCycFormulaSentence(Object... args) {
//this should never actually happen, but when it does, this is what we should do
if (args.length == 1 && args[0] instanceof FormulaSentence) {
return (FormulaSentence) args[0];
}
List