Skip to content

Commit

Permalink
Merge branch 'ImpliedPropertyBugs-AWC'
Browse files Browse the repository at this point in the history
  • Loading branch information
GEGlobalResearch committed Feb 21, 2017
2 parents 6bbafc1 + ff7534b commit b713357
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -248,6 +249,9 @@ public final static Property xsdProperty( String local )

protected enum AnnType {ALIAS, NOTE}

private List<String> comparisonOperators = Arrays.asList(">=",">","<=","<","==","!=","is","=","not","unique","in","contains","does",/*"not",*/"contain");
private List<String> numericOperators = Arrays.asList("*","+","/","-","%","^");
private List<String> canBeNumericOperators = Arrays.asList(">=",">","<=","<","==","!=","is","=");
public enum OPERATORS_RETURNING_BOOLEAN {contains, unique, is, gt, ge, lt, le, and, or, not, was, hasBeen}

public enum BOOLEAN_LITERAL_TEST {BOOLEAN_TRUE, BOOLEAN_FALSE, NOT_BOOLEAN, NOT_BOOLEAN_NEGATED}
Expand Down Expand Up @@ -6203,6 +6207,7 @@ protected boolean isBooleanOperator(String op) {
// return false;
// }
protected Object translateAndApplyImpliedProperty(Expression expr, Property impliedPropertyWrapper) throws InvalidNameException, InvalidTypeException, TranslationException {
int start = serialize.length();
if (includeImpliedPropertiesInDirectWrite && impliedPropertyWrapper != null) {
serialize.append("impliedProperty('");
serialize.append(impliedPropertyWrapper.toString());
Expand All @@ -6212,11 +6217,22 @@ protected Object translateAndApplyImpliedProperty(Expression expr, Property impl
if (includeImpliedPropertiesInDirectWrite && impliedPropertyWrapper != null) {
serialize.append(")");
}
return obj;
if (!(obj instanceof String)) {
return obj;
}
return getExpressionTranslationString(start);
}

protected void resetProcessorState(SadlModelElement element) throws InvalidTypeException {
if (getModelValidator() != null) {
getModelValidator().resetValidatorState(element);
}
}

public List<Equation> getEquations() {
return equations;
}

public void setEquations(List<Equation> equations) {
this.equations = equations;
}
Expand Down Expand Up @@ -6265,7 +6281,12 @@ public List<ConceptName> getImpliedProperties(com.hp.hpl.jena.rdf.model.Resource
retlst = scips;
}
else {
retlst.addAll(scips);
for (int i = 0; i < scips.size(); i++) {
ConceptName cn = scips.get(i);
if (!scips.contains(cn)) {
retlst.add(scips.get(i));
}
}
}
}
}
Expand All @@ -6278,7 +6299,10 @@ public List<ConceptName> getImpliedProperties(com.hp.hpl.jena.rdf.model.Resource
while (sitr.hasNext()) {
RDFNode obj = sitr.nextStatement().getObject();
if (obj.isURIResource()) {
retlst.add(new ConceptName(obj.asResource().getURI()));
ConceptName cn = new ConceptName(obj.asResource().getURI());
if (!retlst.contains(cn)) {
retlst.add(cn);
}
}
}
return retlst;
Expand Down Expand Up @@ -6348,6 +6372,81 @@ protected String conceptIdentifierToString(ConceptIdentifier ci) {
}
return ci.toString();
}

/********************************* End translate methods *****************************************/
protected String getExpressionTranslationString(int start) {
if (start > serialize.length()) {
start = serialize.length();
}
return serialize.substring(start);
}

public boolean isComparisonOperator(String operation) {
if (comparisonOperators.contains(operation)) {
return true;
}
return false;
}

public boolean isBooleanComparison(List<String> operations) {
if(comparisonOperators.containsAll(operations)){
return true;
}
return false;
}

public boolean canBeNumericOperator(String op) {
if (canBeNumericOperators.contains(op)) return true;
return false;
}

public boolean isNumericOperator(String op) {
if (numericOperators.contains(op)) return true;
return false;
}

public boolean isNumericOperator(List<String> operations) {
Iterator<String> itr = operations.iterator();
while (itr.hasNext()) {
if (isNumericOperator(itr.next())) return true;
}
return false;
}

public boolean canBeNumericOperator(List<String> operations) {
Iterator<String> itr = operations.iterator();
while (itr.hasNext()) {
if (canBeNumericOperator(itr.next())) return true;
}
return false;
}

public boolean isNumericType(ConceptName conceptName) {
try {
String uri = conceptName.getUri();
return isNumericType(uri);
} catch (InvalidNameException e) {
e.printStackTrace();
}
return false;
}

public boolean isNumericType(String uri) {
if (uri.equals(XSD.decimal.getURI()) ||
uri.equals(XSD.integer.getURI()) ||
uri.equals(XSD.xdouble.getURI()) ||
uri.equals(XSD.xfloat.getURI()) ||
uri.equals(XSD.xint.getURI()) ||
uri.equals(XSD.xlong.getURI())) {
return true;
}
return false;
}

public boolean isBooleanType(String uri) {
if (uri.equals(XSD.xboolean.getURI())) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.ge.research.sadl.sADL.PropOfSubject;
import com.ge.research.sadl.sADL.SadlDataType;
import com.ge.research.sadl.sADL.SadlIntersectionType;
import com.ge.research.sadl.sADL.SadlModelElement;
import com.ge.research.sadl.sADL.SadlParameterDeclaration;
import com.ge.research.sadl.sADL.SadlPrimitiveDataType;
import com.ge.research.sadl.sADL.SadlPropertyCondition;
Expand Down Expand Up @@ -93,9 +94,6 @@ public class JenaBasedSadlModelValidator implements ISadlModelValidator {
protected ValidationAcceptor issueAcceptor = null;
protected OntModel theJenaModel = null;
protected DeclarationExtensions declarationExtensions = null;
private List<String> comparisonOperators = Arrays.asList(">=",">","<=","<","==","!=","is","=","not","unique","in","contains","does",/*"not",*/"contain");
private List<String> numericOperators = Arrays.asList("*","+","/","-","%","^");
private List<String> canBeNumericOperators = Arrays.asList(">=",">","<=","<","==","!=","is","=");
private EObject defaultContext;

protected Map<EObject, TypeCheckInfo> expressionsValidated = new HashMap<EObject,TypeCheckInfo>();
Expand Down Expand Up @@ -314,7 +312,7 @@ private Object implicitPropertiesToString(List<ConceptName> iprops) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < iprops.size(); i++) {
if (i > 0) sb.append(", ");
sb.append(getImplicitProperties().get(0).toFQString());
sb.append(getImplicitProperties().get(i).toFQString());
}
return sb.toString();
}
Expand Down Expand Up @@ -604,7 +602,7 @@ protected void createErrorMessage(StringBuilder errorMessageBuilder, TypeCheckIn
}
}

if (comparisonOperators.contains(operation)) {
if (getModelProcessor().isComparisonOperator(operation)) {
op = "be compared (" + operation + ")";
}
else {
Expand Down Expand Up @@ -900,7 +898,7 @@ else if(expression instanceof BinaryOperation){
}
TypeCheckInfo binopreturn = combineTypes(operations, ((BinaryOperation) expression).getLeft(), ((BinaryOperation) expression).getRight(),
leftTypeCheckInfo, rightTypeCheckInfo);
if (isNumericOperator(((BinaryOperation) expression).getOp())) {
if (getModelProcessor().isNumericOperator(((BinaryOperation) expression).getOp())) {
if (leftTypeCheckInfo != null && !isNumeric(leftTypeCheckInfo) && !isNumericWithImpliedProperty(leftTypeCheckInfo, ((BinaryOperation)expression).getLeft())) {
issueAcceptor.addError("Numeric operator requires numeric arguments", ((BinaryOperation)expression).getLeft());
}
Expand Down Expand Up @@ -1041,12 +1039,12 @@ else if (prop.canAs(DatatypeProperty.class)) {
return false;
}

private boolean isNumeric(TypeCheckInfo tci) {
private boolean isNumeric(TypeCheckInfo tci) throws InvalidTypeException {
ConceptIdentifier ci;
if (tci.getTypeCheckType() != null) {
ci = tci.getTypeCheckType();
if (ci instanceof ConceptName) {
return isNumericType((ConceptName) ci);
return getModelProcessor().isNumericType((ConceptName) ci);
}
}
else if (tci.getExplicitValueType() != null) {
Expand All @@ -1056,7 +1054,7 @@ else if (tci.getExplicitValueType() != null) {
issueAcceptor.addWarning("Explicit value type is RESTRICITON, which isn't yet handled. Please report with use case.", tci.context);
}
else if (tci.getExpressionType() instanceof ConceptName){
return isNumericType((ConceptName) tci.getExpressionType());
return getModelProcessor().isNumericType((ConceptName) tci.getExpressionType());
}
}
return false;
Expand Down Expand Up @@ -1117,6 +1115,7 @@ else if(constant.equals("None")){
}

private boolean isVariable(TypeCheckInfo tci) {
if (tci == null) return false;
ConceptIdentifier ci = tci.getTypeCheckType();
if (ci instanceof ConceptName && ((ConceptName)ci).getType() != null && ((ConceptName)ci).getType().equals(ConceptType.VARIABLE)) {
return true;
Expand Down Expand Up @@ -1528,9 +1527,19 @@ protected List<OntClass> getTypeCheckTypeClasses(TypeCheckInfo tci) {
return results;
}

protected TypeCheckInfo getTypeFromRestriction(Expression subject, Expression predicate) throws CircularDefinitionException, InvalidTypeException {
protected TypeCheckInfo getTypeFromRestriction(Expression subject, Expression predicate) throws CircularDefinitionException, InvalidTypeException, DontTypeCheckException, InvalidNameException, TranslationException, URISyntaxException, IOException, ConfigurationException, CircularDependencyException {
if (subject instanceof Name && predicate instanceof Name) {
String subjuri = declarationExtensions.getConceptUri(((Name)subject).getName());
OntConceptType subjtype = declarationExtensions.getOntConceptType(((Name)subject).getName());
if (subjtype.equals(OntConceptType.VARIABLE)) {
TypeCheckInfo varTci = getType(((Name)subject).getName());
if (varTci != null && varTci.getTypeCheckType() != null) {
ConceptIdentifier varci = varTci.getTypeCheckType();
if (varci instanceof ConceptName) {
subjuri = ((ConceptName)varci).getUri();
}
}
}
String propuri = declarationExtensions.getConceptUri(((Name)predicate).getName());
OntConceptType proptype = declarationExtensions.getOntConceptType(((Name)predicate).getName());
return getTypeFromRestriction(subjuri, propuri, proptype, predicate);
Expand Down Expand Up @@ -2076,12 +2085,12 @@ private TypeCheckInfo combineTypes(List<String> operations, Expression leftExpre
if(!compareTypes(operations, leftExpression, rightExpression, leftTypeCheckInfo, rightTypeCheckInfo)){
return null;
}
if (isBooleanComparison(operations)) {
if (getModelProcessor().isBooleanComparison(operations)) {
ConceptName booleanLiteralConceptName = new ConceptName(XSD.xboolean.getURI());
booleanLiteralConceptName.setType(ConceptType.DATATYPEPROPERTY);
return new TypeCheckInfo(booleanLiteralConceptName, booleanLiteralConceptName, this, leftExpression.eContainer());
}
else if (isNumericOperator(operations)) {
else if (getModelProcessor().isNumericOperator(operations)) {
ConceptName lcn = getTypeCheckInfoType(leftTypeCheckInfo);
ConceptName rcn = getTypeCheckInfoType(rightTypeCheckInfo);
if (lcn == null || lcn.getNamespace() == null) {
Expand Down Expand Up @@ -2155,13 +2164,6 @@ private ConceptName getTypeCheckInfoType(TypeCheckInfo tci) throws InvalidNameEx
throw new InvalidNameException("Failed to get TypeCheckInfoType");
}

protected boolean isBooleanComparison(List<String> operations) {
if(comparisonOperators.containsAll(operations)){
return true;
}
return false;
}

/**
* Compare two TypeCheckInfo structures
* @param operations
Expand Down Expand Up @@ -2266,6 +2268,8 @@ else if (val.isLiteral()) {

private boolean compareTypesUsingImpliedProperties(List<String> operations, EObject leftExpression,
EObject rightExpression, TypeCheckInfo leftTypeCheckInfo, TypeCheckInfo rightTypeCheckInfo) throws InvalidNameException, DontTypeCheckException, InvalidTypeException {

String opstr = (operations != null && operations.size() > 0) ? operations.get(0) : null;
if (leftTypeCheckInfo.getImplicitProperties() != null) {
Iterator<ConceptName> litr = leftTypeCheckInfo.getImplicitProperties().iterator();
while (litr.hasNext()) {
Expand All @@ -2282,7 +2286,7 @@ else if (prop.canAs(DatatypeProperty.class)) {
}
TypeCheckInfo newltci = getTypeInfoFromRange(cn, prop, leftExpression);
if (compareTypes(operations, leftExpression, rightExpression, newltci, rightTypeCheckInfo)) {
issueAcceptor.addInfo("Implied property '" + getModelProcessor().conceptIdentifierToString(cn) + "' used (left side) to pass type check", leftExpression);
issueAcceptor.addInfo("Implied property '" + getModelProcessor().conceptIdentifierToString(cn) + "' used (left side" + (opstr != null ? (" of '" + opstr + "'") : "") + ") to pass type check", leftExpression);
addImpliedPropertiesUsed(leftExpression, prop);
return true;
}
Expand All @@ -2304,7 +2308,7 @@ else if (prop.canAs(DatatypeProperty.class)) {
}
TypeCheckInfo newrtci = getTypeInfoFromRange(cn, prop, rightExpression);
if (compareTypes(operations, leftExpression, rightExpression, leftTypeCheckInfo, newrtci)) {
issueAcceptor.addInfo("Implied property '" + getModelProcessor().conceptIdentifierToString(cn) + "' used (right side) to pass type check", rightExpression);
issueAcceptor.addInfo("Implied property '" + getModelProcessor().conceptIdentifierToString(cn) + "' used (right side" + (opstr != null ? (" of '" + opstr + "'") : "") + ") to pass type check", rightExpression);
addImpliedPropertiesUsed(rightExpression, prop);
return true;
}
Expand Down Expand Up @@ -2333,14 +2337,14 @@ private boolean compatibleTypes(List<String> operations, EObject leftExpression,
ConceptName leftConceptName = (ConceptName) leftConceptIdentifier;
ConceptName rightConceptName = (ConceptName) rightConceptIdentifier;

if (isNumericOperator(operations) && (!isNumeric(leftTypeCheckInfo) || !isNumeric(rightTypeCheckInfo))) {
if (getModelProcessor().isNumericOperator(operations) && (!isNumeric(leftTypeCheckInfo) || !isNumeric(rightTypeCheckInfo))) {
return false;
}
if (leftConceptName.equals(rightConceptName)) {
return true;
}
else if ((isNumericOperator(operations) || canBeNumericOperator(operations)) &&
(isNumericType(leftConceptName) && isNumericType(rightConceptName))) {
else if ((getModelProcessor().isNumericOperator(operations) || getModelProcessor().canBeNumericOperator(operations)) &&
(getModelProcessor().isNumericType(leftConceptName) && getModelProcessor().isNumericType(rightConceptName))) {
return true;
}
else if (leftConceptName.getType() == null || rightConceptName.getType() == null) {
Expand Down Expand Up @@ -2504,48 +2508,6 @@ private <X extends EObject> boolean checkForContainer(EObject expr, Class<X> t )
return checkForContainer(expr.eContainer(), t);
}

private boolean canBeNumericOperator(List<String> operations) {
Iterator<String> itr = operations.iterator();
while (itr.hasNext()) {
if (canBeNumericOperator(itr.next())) return true;
}
return false;
}

private boolean canBeNumericOperator(String op) {
if (canBeNumericOperators.contains(op)) return true;
return false;
}

private boolean isNumericOperator(String op) {
if (numericOperators.contains(op)) return true;
return false;
}

private boolean isNumericOperator(List<String> operations) {
Iterator<String> itr = operations.iterator();
while (itr.hasNext()) {
if (isNumericOperator(itr.next())) return true;
}
return false;
}

private boolean isNumericType(ConceptName conceptName) {
try {
if (conceptName.getUri().equals(XSD.decimal.getURI()) ||
conceptName.getUri().equals(XSD.integer.getURI()) ||
conceptName.getUri().equals(XSD.xdouble.getURI()) ||
conceptName.getUri().equals(XSD.xfloat.getURI()) ||
conceptName.getUri().equals(XSD.xint.getURI()) ||
conceptName.getUri().equals(XSD.xlong.getURI())) {
return true;
}
} catch (InvalidNameException e) {
e.printStackTrace();
}
return false;
}

protected boolean isQualifyingListOperation(List<String> operations, TypeCheckInfo leftTypeCheckInfo, TypeCheckInfo rightTypeCheckInfo) {
if (operations.contains("contain") || operations.contains("contains") && leftTypeCheckInfo != null &&
leftTypeCheckInfo.getRangeValueType().equals(RangeValueType.LIST)) {
Expand Down Expand Up @@ -2845,4 +2807,16 @@ protected void setModelProcessor(JenaBasedSadlModelProcessor modelProcessor) {
this.modelProcessor = modelProcessor;
}

public void resetValidatorState(SadlModelElement element) {
if (impliedPropertiesUsed != null) {
impliedPropertiesUsed.clear();
}
if (binaryLeftTypeCheckInfo != null) {
binaryLeftTypeCheckInfo.clear();
}
if (binaryRightTypeCheckInfo != null) {
binaryRightTypeCheckInfo.clear();
}
}

}
Loading

0 comments on commit b713357

Please sign in to comment.