📄 objectcreation.java
字号:
names.add( "p1" ); ObjectType type = TypeFactory.mkObjectType( model.getClass( "Person" ) ); MCmd cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of an object (c1) of the class Company names.clear(); names.add( "c1" ); type = TypeFactory.mkObjectType( model.getClass( "Company" ) ); cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of a link between p1 and c1 of an association names.clear(); names.add( "p1" ); names.add( "c1" ); MAssociationClass assoc = model.getAssociationClass( "Job" ); cmd = new MCmdCreateInsertObjects( system.state(), "j1", assoc, names ); system.executeCmd( cmd ); // set an attribute value in c1 ExpConstString expr = new ExpConstString( "IBM" ); MObject obj = system.state().objectByName( "c1" ); MAttribute attr = obj.cls().attribute( "name", false ); ExpVariable exprVar = new ExpVariable( obj.name(), obj.type() ); ExpAttrOp attrOpExp = new ExpAttrOp( attr, exprVar ); cmd = new MCmdSetAttribute( system.state(), attrOpExp, expr ); system.executeCmd( cmd ); return system; } catch ( Exception e ) { throw ( new Error( e ) ); } } /** * Creates an instance of a model with one class and one associationclass. * * @return returns the actual System. */ public MSystem createModelWithObjectsOfSameClassAndLinkObject() { try { // creation of the system MModel model = TestModelUtil.getInstance() .createModelWithOneClassAndOneAssocClass(); MSystem system = new MSystem( model ); // creation of an objects (p1,p2) of the class Person List names = new ArrayList(); names.add( "p1" ); names.add( "p2" ); ObjectType type = TypeFactory.mkObjectType( model.getClass( "Person" ) ); MCmd cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of a link between p1 and p2 of an associationclass names.clear(); names.add( "p1" ); names.add( "p2" ); MAssociationClass assoc = model.getAssociationClass( "Job" ); cmd = new MCmdCreateInsertObjects( system.state(), "j1", assoc, names ); system.executeCmd( cmd ); return system; } catch ( Exception e ) { throw ( new Error( e ) ); } } /** * Creates a model with two classes and an associationclass. It creates * instances of those as well. * * @return returns the actual System. */ public MSystem createModelWithObjectsAndTenaryLinkObject() { try { // creation of the system MModel model = TestModelUtil.getInstance() .createModelWithClassAndTenaryAssocClass(); MSystem system = new MSystem( model ); // creation of an object (p1) of the class Person List names = new ArrayList(); names.add( "p1" ); ObjectType type = TypeFactory.mkObjectType( model.getClass( "Person" ) ); MCmd cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of an object (c1) of the class Company names.clear(); names.add( "c1" ); type = TypeFactory.mkObjectType( model.getClass( "Company" ) ); cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of an object (s1) of the class Company names.clear(); names.add( "s1" ); type = TypeFactory.mkObjectType( model.getClass( "Salary" ) ); cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of a link between p1, s1 and c1 of an association names.clear(); names.add( "p1" ); names.add( "c1" ); names.add( "s1" ); MAssociationClass assoc = model.getAssociationClass( "Job" ); cmd = new MCmdCreateInsertObjects( system.state(), "j1", assoc, names ); system.executeCmd( cmd ); // set an attribute value in c1 ExpConstString expr = new ExpConstString( "IBM" ); MObject obj = system.state().objectByName( "c1" ); MAttribute attr = obj.cls().attribute( "name", false ); ExpVariable exprVar = new ExpVariable( obj.name(), obj.type() ); ExpAttrOp attrOpExp = new ExpAttrOp( attr, exprVar ); cmd = new MCmdSetAttribute( system.state(), attrOpExp, expr ); system.executeCmd( cmd ); return system; } catch ( Exception e ) { throw ( new Error( e ) ); } } /** * Creates a model with two classes, an associationclass and an association. * It creates instances of those as well. * * @return returns the actual System. */ public MSystem createModelWithObjectsAndLinkObject2() { try { // creation of the system MModel model = TestModelUtil.getInstance() .createComplexModel(); MSystem system = new MSystem( model ); // creation of an object (p1) of the class Person List names = new ArrayList(); names.add( "p1" ); ObjectType type = TypeFactory.mkObjectType( model.getClass( "Person" ) ); MCmd cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of an object (p2) of the class Person names.clear(); names.add( "p2" ); type = TypeFactory.mkObjectType( model.getClass( "Person" ) ); cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of a link between p1 and p2 (p1 is boss of p2) names.clear(); names.add( "p1" ); names.add( "p2" ); Expression[] exprs = new Expression[names.size()]; Iterator it = names.iterator(); int i = 0; while (it.hasNext() ) { MObject obj = system.state().objectByName( (String) it.next() ); exprs[i++] = new ExpVariable( obj.name(), obj.type() ); } MAssociation ass = model.getAssociation( "isBoss" ); cmd = new MCmdInsertLink( system.state(), exprs, ass ); system.executeCmd( cmd ); // creation of an object (c1) of the class Company names.clear(); names.add( "c1" ); type = TypeFactory.mkObjectType( model.getClass( "Company" ) ); cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of an object (c2) of the class Company names.clear(); names.add( "c2" ); type = TypeFactory.mkObjectType( model.getClass( "Company" ) ); cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of a link object between p1 and c1 of an association names.clear(); names.add( "p1" ); names.add( "c1" ); MAssociationClass assoc = model.getAssociationClass( "Job" ); cmd = new MCmdCreateInsertObjects( system.state(), "j1", assoc, names ); system.executeCmd( cmd ); // creation of a link object between p2 and c1 of an association names.clear(); names.add( "p2" ); names.add( "c1" ); assoc = model.getAssociationClass( "Job" ); cmd = new MCmdCreateInsertObjects( system.state(), "j2", assoc, names ); system.executeCmd( cmd ); // set an attribute value in c1 ExpConstString expr = new ExpConstString( "IBM" ); MObject obj = system.state().objectByName( "c1" ); MAttribute attr = obj.cls().attribute( "name", false ); ExpVariable exprVar = new ExpVariable( obj.name(), obj.type() ); ExpAttrOp attrOpExp = new ExpAttrOp( attr, exprVar ); cmd = new MCmdSetAttribute( system.state(), attrOpExp, expr ); system.executeCmd( cmd ); // set an attribute value in c2 expr = new ExpConstString( "SUN" ); obj = system.state().objectByName( "c2" ); attr = obj.cls().attribute( "name", false ); exprVar = new ExpVariable( obj.name(), obj.type() ); attrOpExp = new ExpAttrOp( attr, exprVar ); cmd = new MCmdSetAttribute( system.state(), attrOpExp, expr ); system.executeCmd( cmd ); return system; } catch ( Exception e ) { throw ( new Error( e ) ); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -