src/main/java/org/semgus/java/event/EventParser.java
private static SpecEvent parseHornClause(JSONObject eventDto) throws DeserializationException {
        JSONObject constructorDto = JsonUtils.getObject(eventDto, "constructor");

        // parse constructor specification
        String constructorName, returnType;
        List<String> constructorArgs;
        JSONArray constructorArgTypesDto;
        try {
            constructorName = JsonUtils.getString(constructorDto, "name");
            returnType = JsonUtils.getString(constructorDto, "returnSort");
            constructorArgs = JsonUtils.getStrings(constructorDto, "arguments");
            constructorArgTypesDto = JsonUtils.getArray(constructorDto, "argumentSorts");
        } catch (DeserializationException e) {
            throw e.prepend("constructor");
        }
        if (constructorArgs.size() != constructorArgTypesDto.size()) { // ensure args and arg types coincide
            throw new DeserializationException(
                    String.format(
                            "Argument sorts and arguments of CHC constructor have different lengths %d != %d",
                            constructorArgTypesDto.size(), constructorArgs.size()),
                    "constructor");
        }

        // parse constructor arg type identifiers
        Identifier[] constructorArgTypes = new Identifier[constructorArgTypesDto.size()];
        for (int i = 0; i < constructorArgTypes.length; i++) {
            try {
                constructorArgTypes[i] = Identifier.deserialize(constructorArgTypesDto.get(i));
            } catch (DeserializationException e) {
                throw e.prepend("constructor.argumentSorts." + i);
            }
        }
        HornClauseEvent.Constructor constructor = new HornClauseEvent.Constructor(
                constructorName,
                TypedVar.fromNamesAndTypes(constructorArgs, Arrays.asList(constructorArgTypes)),
                returnType);

        // parse head relation
        JSONObject headDto = JsonUtils.getObject(eventDto, "head");
        RelationApp head;
        try {
            head = RelationApp.deserialize(headDto);
        } catch (DeserializationException e) {
            throw e.prepend("head");
        }

        // parse body relations
        List<JSONObject> bodyRelationsDto = JsonUtils.getObjects(eventDto, "bodyRelations");
        RelationApp[] bodyRelations = new RelationApp[bodyRelationsDto.size()];
        for (int i = 0; i < bodyRelations.length; i++) {
            try {
                bodyRelations[i] = RelationApp.deserialize(bodyRelationsDto.get(i));
            } catch (DeserializationException e) {
                throw e.prepend("bodyRelations." + i);
            }
        }

        // parse semantic constraint
        Object constraintDtoRaw = JsonUtils.get(eventDto, "constraint");
        SmtTerm constraint;
        try {
            constraint = SmtTerm.deserialize(constraintDtoRaw);
        } catch (DeserializationException e) {
            throw e.prepend("constraint");
        }

        // parse variable list
        List<String> variablesDto = JsonUtils.getStrings(eventDto, "variables");
        Map<String, AnnotatedVar> variables = new HashMap<>();
        for (int i = 0; i < variablesDto.size(); i++) {
            String variable = variablesDto.get(i);
            if (variables.containsKey(variable)) {
                throw new DeserializationException(
                        String.format("Duplicate variable \"%s\"", variable), "variables." + i);
            }
            variables.put(variable, new AnnotatedVar(variable, new HashMap<>()));
        }

        // parse input variable annotations
        Object inputVariablesRaw = eventDto.get("inputVariables");
        if (inputVariablesRaw != null) {
            // if present, ensure it's an array of strings
            if (!(inputVariablesRaw instanceof JSONArray)) {
                throw new DeserializationException("Input variable list must be a JSON array!", "inputVariables");
            }
            List<String> inputVariables = JsonUtils.ensureStrings((JSONArray)inputVariablesRaw);

            // annotate the listed variables
            for (int i = 0; i < inputVariables.size(); i++) {
                AnnotatedVar variable = variables.get(inputVariables.get(i));
                if (variable == null) {
                    throw new DeserializationException(
                            String.format("Unknown variable \"%s\" declared as input", inputVariables.get(i)),
                            "inputVariables." + i);
                }
                variable.attributes().put("input", new AttributeValue.Unit());
            }
        }

        // ditto for output variable annotations
        Object outputVariablesRaw = eventDto.get("outputVariables");
        if (outputVariablesRaw != null) {
            // if present, ensure it's an array of strings
            if (!(outputVariablesRaw instanceof JSONArray)) {
                throw new DeserializationException("Output variable list must be a JSON array!", "outputVariables");
            }
            List<String> outputVariables = JsonUtils.ensureStrings((JSONArray)outputVariablesRaw);

            // annotate the listed variables
            for (int i = 0; i < outputVariables.size(); i++) {
                AnnotatedVar variable = variables.get(outputVariables.get(i));
                if (variable == null) {
                    throw new DeserializationException(
                            String.format("Unknown variable \"%s\" declared as output", outputVariables.get(i)),
                            "outputVariables." + i);
                }
                variable.attributes().put("output", new AttributeValue.Unit());
            }
        }

        return new HornClauseEvent(
                constructor, head, Arrays.asList(bodyRelations), constraint, variables);
    }
/**
     * Deserializes a "chc" event.
     *
     * @param eventDto The JSON representation of the event.
     * @return The deserialized event.
     * @throws DeserializationException If {@code eventDto} is not a valid representation of a "chc" event.
     */
Deserializes a "chc" event.
@param eventDto The JSON representation of the event.
@return The deserialized event.
@throws DeserializationException If  eventDto is not a valid representation of a "chc" event.
Deserializes a " chc " event . @param eventDto The JSON representation of the event . @return The deserialized event . @throws DeserializationException If eventDto is not a valid representation of a " chc " event .
private static SpecEvent parseHornClause(JSONObject eventDto) throws DeserializationException {
        JSONObject constructorDto = JsonUtils.getObject(eventDto, "constructor");
        String constructorName, returnType;
        List<String> constructorArgs;
        JSONArray constructorArgTypesDto;
        try {
            constructorName = JsonUtils.getString(constructorDto, "name");
            returnType = JsonUtils.getString(constructorDto, "returnSort");
            constructorArgs = JsonUtils.getStrings(constructorDto, "arguments");
            constructorArgTypesDto = JsonUtils.getArray(constructorDto, "argumentSorts");
        } catch (DeserializationException e) {
            throw e.prepend("constructor");
        }
        if (constructorArgs.size() != constructorArgTypesDto.size()) { 
            throw new DeserializationException(
                    String.format(
                            "Argument sorts and arguments of CHC constructor have different lengths %d != %d",
                            constructorArgTypesDto.size(), constructorArgs.size()),
                    "constructor");
        }
        Identifier[] constructorArgTypes = new Identifier[constructorArgTypesDto.size()];
        for (int i = 0; i < constructorArgTypes.length; i++) {
            try {
                constructorArgTypes[i] = Identifier.deserialize(constructorArgTypesDto.get(i));
            } catch (DeserializationException e) {
                throw e.prepend("constructor.argumentSorts." + i);
            }
        }
        HornClauseEvent.Constructor constructor = new HornClauseEvent.Constructor(
                constructorName,
                TypedVar.fromNamesAndTypes(constructorArgs, Arrays.asList(constructorArgTypes)),
                returnType);
        JSONObject headDto = JsonUtils.getObject(eventDto, "head");
        RelationApp head;
        try {
            head = RelationApp.deserialize(headDto);
        } catch (DeserializationException e) {
            throw e.prepend("head");
        }
        List<JSONObject> bodyRelationsDto = JsonUtils.getObjects(eventDto, "bodyRelations");
        RelationApp[] bodyRelations = new RelationApp[bodyRelationsDto.size()];
        for (int i = 0; i < bodyRelations.length; i++) {
            try {
                bodyRelations[i] = RelationApp.deserialize(bodyRelationsDto.get(i));
            } catch (DeserializationException e) {
                throw e.prepend("bodyRelations." + i);
            }
        }
        Object constraintDtoRaw = JsonUtils.get(eventDto, "constraint");
        SmtTerm constraint;
        try {
            constraint = SmtTerm.deserialize(constraintDtoRaw);
        } catch (DeserializationException e) {
            throw e.prepend("constraint");
        }
        List<String> variablesDto = JsonUtils.getStrings(eventDto, "variables");
        Map<String, AnnotatedVar> variables = new HashMap<>();
        for (int i = 0; i < variablesDto.size(); i++) {
            String variable = variablesDto.get(i);
            if (variables.containsKey(variable)) {
                throw new DeserializationException(
                        String.format("Duplicate variable \"%s\"", variable), "variables." + i);
            }
            variables.put(variable, new AnnotatedVar(variable, new HashMap<>()));
        }
        Object inputVariablesRaw = eventDto.get("inputVariables");
        if (inputVariablesRaw != null) {
            if (!(inputVariablesRaw instanceof JSONArray)) {
                throw new DeserializationException("Input variable list must be a JSON array!", "inputVariables");
            }
            List<String> inputVariables = JsonUtils.ensureStrings((JSONArray)inputVariablesRaw);
            for (int i = 0; i < inputVariables.size(); i++) {
                AnnotatedVar variable = variables.get(inputVariables.get(i));
                if (variable == null) {
                    throw new DeserializationException(
                            String.format("Unknown variable \"%s\" declared as input", inputVariables.get(i)),
                            "inputVariables." + i);
                }
                variable.attributes().put("input", new AttributeValue.Unit());
            }
        }
        Object outputVariablesRaw = eventDto.get("outputVariables");
        if (outputVariablesRaw != null) {
            if (!(outputVariablesRaw instanceof JSONArray)) {
                throw new DeserializationException("Output variable list must be a JSON array!", "outputVariables");
            }
            List<String> outputVariables = JsonUtils.ensureStrings((JSONArray)outputVariablesRaw);
            for (int i = 0; i < outputVariables.size(); i++) {
                AnnotatedVar variable = variables.get(outputVariables.get(i));
                if (variable == null) {
                    throw new DeserializationException(
                            String.format("Unknown variable \"%s\" declared as output", outputVariables.get(i)),
                            "outputVariables." + i);
                }
                variable.attributes().put("output", new AttributeValue.Unit());
            }
        }
        return new HornClauseEvent(
                constructor, head, Arrays.asList(bodyRelations), constraint, variables);
    }
private static SpecEvent parseHornClause ( JSONObject eventDto ) throws DeserializationException { JSONObject constructorDto = JsonUtils . getObject ( eventDto , "constructor" ) ; String constructorName , returnType ; List < String > constructorArgs ; JSONArray constructorArgTypesDto ; try { constructorName = JsonUtils . getString ( constructorDto , "name" ) ; returnType = JsonUtils . getString ( constructorDto , "returnSort" ) ; constructorArgs = JsonUtils . getStrings ( constructorDto , "arguments" ) ; constructorArgTypesDto = JsonUtils . getArray ( constructorDto , "argumentSorts" ) ; } catch ( DeserializationException e ) { throw e . prepend ( "constructor" ) ; } if ( constructorArgs . size ( ) != constructorArgTypesDto . size ( ) ) { throw new DeserializationException ( String . format ( "Argument sorts and arguments of CHC constructor have different lengths %d != %d" , constructorArgTypesDto . size ( ) , constructorArgs . size ( ) ) , "constructor" ) ; } Identifier [ ] constructorArgTypes = new Identifier [ constructorArgTypesDto . size ( ) ] ; for ( int i = 0 ; i < constructorArgTypes . length ; i ++ ) { try { constructorArgTypes [ i ] = Identifier . deserialize ( constructorArgTypesDto . get ( i ) ) ; } catch ( DeserializationException e ) { throw e . prepend ( "constructor.argumentSorts." + i ) ; } } HornClauseEvent . Constructor constructor = new HornClauseEvent . Constructor ( constructorName , TypedVar . fromNamesAndTypes ( constructorArgs , Arrays . asList ( constructorArgTypes ) ) , returnType ) ; JSONObject headDto = JsonUtils . getObject ( eventDto , "head" ) ; RelationApp head ; try { head = RelationApp . deserialize ( headDto ) ; } catch ( DeserializationException e ) { throw e . prepend ( "head" ) ; } List < JSONObject > bodyRelationsDto = JsonUtils . getObjects ( eventDto , "bodyRelations" ) ; RelationApp [ ] bodyRelations = new RelationApp [ bodyRelationsDto . size ( ) ] ; for ( int i = 0 ; i < bodyRelations . length ; i ++ ) { try { bodyRelations [ i ] = RelationApp . deserialize ( bodyRelationsDto . get ( i ) ) ; } catch ( DeserializationException e ) { throw e . prepend ( "bodyRelations." + i ) ; } } Object constraintDtoRaw = JsonUtils . get ( eventDto , "constraint" ) ; SmtTerm constraint ; try { constraint = SmtTerm . deserialize ( constraintDtoRaw ) ; } catch ( DeserializationException e ) { throw e . prepend ( "constraint" ) ; } List < String > variablesDto = JsonUtils . getStrings ( eventDto , "variables" ) ; Map < String , AnnotatedVar > variables = new HashMap < > ( ) ; for ( int i = 0 ; i < variablesDto . size ( ) ; i ++ ) { String variable = variablesDto . get ( i ) ; if ( variables . containsKey ( variable ) ) { throw new DeserializationException ( String . format ( "Duplicate variable \"%s\"" , variable ) , "variables." + i ) ; } variables . put ( variable , new AnnotatedVar ( variable , new HashMap < > ( ) ) ) ; } Object inputVariablesRaw = eventDto . get ( "inputVariables" ) ; if ( inputVariablesRaw != null ) { if ( ! ( inputVariablesRaw instanceof JSONArray ) ) { throw new DeserializationException ( "Input variable list must be a JSON array!" , "inputVariables" ) ; } List < String > inputVariables = JsonUtils . ensureStrings ( ( JSONArray ) inputVariablesRaw ) ; for ( int i = 0 ; i < inputVariables . size ( ) ; i ++ ) { AnnotatedVar variable = variables . get ( inputVariables . get ( i ) ) ; if ( variable == null ) { throw new DeserializationException ( String . format ( "Unknown variable \"%s\" declared as input" , inputVariables . get ( i ) ) , "inputVariables." + i ) ; } variable . attributes ( ) . put ( "input" , new AttributeValue . Unit ( ) ) ; } } Object outputVariablesRaw = eventDto . get ( "outputVariables" ) ; if ( outputVariablesRaw != null ) { if ( ! ( outputVariablesRaw instanceof JSONArray ) ) { throw new DeserializationException ( "Output variable list must be a JSON array!" , "outputVariables" ) ; } List < String > outputVariables = JsonUtils . ensureStrings ( ( JSONArray ) outputVariablesRaw ) ; for ( int i = 0 ; i < outputVariables . size ( ) ; i ++ ) { AnnotatedVar variable = variables . get ( outputVariables . get ( i ) ) ; if ( variable == null ) { throw new DeserializationException ( String . format ( "Unknown variable \"%s\" declared as output" , outputVariables . get ( i ) ) , "outputVariables." + i ) ; } variable . attributes ( ) . put ( "output" , new AttributeValue . Unit ( ) ) ; } } return new HornClauseEvent ( constructor , head , Arrays . asList ( bodyRelations ) , constraint , variables ) ; }
Deserializes a "chc" event.
Deserializes a " chc " event .
// parse constructor specification // ensure args and arg types coincide // parse constructor arg type identifiers // parse head relation // parse body relations // parse semantic constraint // parse variable list // parse input variable annotations // if present, ensure it's an array of strings // annotate the listed variables // ditto for output variable annotations // if present, ensure it's an array of strings // annotate the listed variables